Ot Aiops
支持多协议工业设备数据采集与智能诊断,具备高风险写入防护机制。
基于Python的网页数据提取工具,支持分页、反反爬及JavaScript渲染页面抓取。
openclaw skills install @ericlooi504/python-web-scraper命令、参数、文件名以原文为准
用于数据提取、分页处理、反屏蔽技术、JavaScript 重度网站的 Selenium 支持以及结构化输出的 Python 网络爬虫工具包。涵盖合乎道德的爬取实践。当 Codex 需要从网站提取数据、处理分页、绕过简单的反机器人措施或抓取 JavaScript 渲染内容时使用。
pip install requests beautifulsoup4 lxml
# 对于 JavaScript 重度网站:
pip install selenium webdriver-manager# 从页面中提取所有链接
python3 scripts/scrape-basic.py https://example.com \
--selector "a[href]" --attr href --output links.json --pretty
# 提取文章中的文本
python3 scripts/scrape-basic.py https://news.ycombinator.com \
--selector ".titleline a" --output hn.txt# URL 参数分页(?page=1, ?page=2)
python3 scripts/scrape-pagination.py https://books.toscrape.com/catalogue/page-1.html \
--selector "h3 a" --attr title --max-pages 5
# 下一页链接检测
python3 scripts/scrape-pagination.py https://quotes.toscrape.com \
--selector "span.text" --max-pages 3python3 scripts/scrape-with-selenium.py https://example.com \
--selector ".dynamic-content" --wait 5 --output data.json轮换 User-Agent 并添加延迟以避免被 429 或封禁:
import random
import time
headers = {
"User-Agent": random.choice(USER_AGENTS),
"Accept": "text/html,application/xhtml+xml",
"Accept-Language": "en-US,en;q=0.9",
"Referer": "https://www.google.com/",
}
time.sleep(random.uniform(1.0, 3.0)) # 在请求间随机延迟对于更严格的屏蔽:可设置 Cookie、使用会话(Session)或添加代理。
首先检查:数据是否已嵌入页面源码中?
import re, json
# 在 <script> 标签中查找 JSON 数据
match = re.search(r'window\.__INITIAL_STATE__\s*=\s*({.*?});', html)
if match:
data = json.loads(match.group(1))许多 SPA(React/Vue)会将数据嵌入 script 标签中——此时可能无需 Selenium。
# 方式一:从浏览器导出 Cookie
# 在浏览器控制台中:document.cookie 或使用 EditThisCookie 插件
# 方式二:使用 requests Session
python3 -c "
import requests
s = requests.Session()
s.post('https://example.com/login', data={'user': '...', 'pass': '...'})
with open('cookies.txt', 'w') as f:
f.write(str(s.cookies.get_dict()))
"脚本默认输出 JSON。转换为 CSV:
# 使用 jq 将 JSON 转为 CSV
python3 scrape-basic.py https://example.com -s "tr" -o data.json --pretty
python3 -c "
import json, csv
with open('data.json') as f:
data = json.load(f)
with open('data.csv', 'w', newline='') as f:
w = csv.writer(f)
w.writerow(['item'])
for d in data:
w.writerow([d])
"robots.txt:https://example.com/robots.txtCrawl-delay 指令scripts/scrape-basic.py** —— 使用 CSS 选择器进行单页抓取,支持 JSON/CSV/文本输出scripts/scrape-pagination.py** —— 分页抓取(URL 参数 + 下一页链接检测)scripts/scrape-with-selenium.py** —— 基于 Selenium 的抓取,适用于 JavaScript 重度网站并支持滚动references/anti-blocking.md** —— 详细的反屏蔽策略与代理使用说明已收录 3 个 Skill