News Crawler Engineering
Use this skill whenever working on news/information crawlers. Treat each site as one source in a long-running news collection system, not as a one-off page parser.
Workflow
Recon first:
- Find the real list API, pagination params, column/channel IDs, detail URL rule.
- Check whether list API returns title, timestamp, real source, images, summary, and full content.
- Inspect page source and JS for
ajax, fetch, page, pageNum, columnId, channel.
- Use browser Network and
user-js-reverse MCP when JS/signature/cookie logic is involved.
- Do not implement before confirming pagination and the real data entry point.
Pick collection mode:
- API includes full content: use API as primary source; fetch detail only to enrich missing fields.
- API is list-only: use API for list/pagination, detail page for content.
- Static HTML only: parse paginated HTML + details.
- Strong CSR/anti-bot: reverse API/signature first, then replay.
Design data model before coding:
Field Rules
media_name: collection site name.
original_source: true source displayed by the article, empty string if unavailable.
summary: article-specific summary only; never store fixed site description, column intro, or template text.
author: do not use editor as a fake author.
stock_list: 文章关联的个股代码列表,格式为纯字符串数组 ["AAPL", "INTC"]。页面或接口有就提取,没有就存空数组 []。不要存对象数组,不要附带URL等额外信息。
crawl_error: short error only; do not store full HTML/response bodies.
- Site-specific lightweight fields go to
extra_data; large/raw fields go to the content table.
- Content success rule:
- Text content exists: success.
- No text but
images is non-empty: success; keep content empty and store content_html if available.
- No text and no images:
crawl_status = 4 / body empty.
Content Extraction
Prefer structured API data over HTML parsing for content extraction.
Data source priority:
- API blocks/paragraphs/segments (most reliable, exact paragraph boundaries).
- HTML detail page with known content container selector (fallback).
- Full-page
get_text() split by newline (last resort).
Paragraph rules:
- Separate paragraphs with
\n\n (double newline), not single \n.
- When using API blocks, each text block = one paragraph; concatenate inline tokens within a block.
- If the API blocks omit the article intro/teaser (common pattern), prepend
description as the first paragraph.
- When falling back to HTML: prefer
<p> tags; also consider <h2>–<h4>, <blockquote>, <li> as paragraph-level elements. Skip non-content areas (disclaimers, author bios, related articles) by scoping to the correct content container first.
- If no
<p> tags exist, fall back to splitting get_text("\n") and filtering empty lines.
Image position in content:
- When the source provides block-structured data with interleaved text and image blocks, insert a placeholder
[图片:caption] in content at the image's position to preserve the spatial relationship between text and images.
images field stores the full URL list (ordered by appearance).
content_html preserves the original HTML structure for re-rendering if needed.
Why this matters:
- Modern sites (React/Next.js SSR) often return incomplete HTML (images rendered by JS, content behind hydration). The API is the source of truth.
- HTTP headers cannot contain non-ASCII characters. When the detail page URL or Referer contains Chinese/Unicode, URL-encode it (
urllib.parse.quote).
Incremental and Backfill
News crawlers must support first-time backfill, daily incremental collection, failed retry, batched dedupe, and resumability.
Standard strategy:
- Query
MAX(publish_time) per source + column_name.
- If no history exists, threshold is
now - backfill_days (default 90 days).
- If history exists, threshold is
max_publish_time - overlap_hours (default 6 hours).
- Iterate by column and page.
- For each page, batch-dedupe candidates with
IN (...).
- Fetch/parse details for that page.
- Immediately upsert that page into DB.
- Stop paging when the whole page is older than threshold.
Never collect all columns/all historical pages into memory and only insert at the end.
Storage and Status
- Write main and content tables in the same transaction.
- Use
INSERT ... ON DUPLICATE KEY UPDATE.
- Failed records should also be stored for retry and diagnosis.
crawl_status:
1: success
2: detail request failed
3: page structure abnormal
4: body empty
5: parse exception
Code Comments
File header: every crawler script must include a module docstring with @Desc followed by a 来源url: line pointing to the target page URL. This makes it easy to locate the crawl target when debugging.
"""
@Author: you name
@File : 香港01财经快讯.py python3
@Desc : 香港01(www.hk01.com 繁体站)財經快訊抓取
来源url:https://www.hk01.com/channel/396/%E8%B2%A1%E7%B6%93%E5%BF%AB%E8%A8%8A
"""
Add concise comments to non-obvious logic. Do not narrate what code does line-by-line. Focus on:
- Method docstrings: one-line summary of purpose and return value. Example:
"""获取增量抓取的时间阈值:表里有数据则取最新时间-overlap_hours,无数据则回溯backfill_days天"""
- Branch intent: explain why a branch exists when the condition alone isn't self-explanatory. Example:
if not self.db_pool: # 本地调试无DB时直接走全量回溯
- Business logic: threshold calculation, stop conditions, dedup strategy. Example:
should_stop = (not page_has_newer) # 本页全部早于阈值则停止翻页
- Non-obvious fallbacks: when code has multiple data source priorities or fallback paths, annotate the intent. Example:
if not content_text: # blocks为空时兜底用HTML提取
Do NOT comment:
- Imports, variable assignments, obvious operations.
- What a function call does (the function name should be self-documenting).
Monitoring & Alerting
每个爬虫必须内置监控报警,不能依赖外部监控平台单独配置。
三层报警(必须全部实现):
- 程序崩溃:未捕获异常 → 飞书/通知 "程序报错"。
- 零数据入库:
total_success == 0 → 飞书/通知 "0条数据入库,请检查网站或API是否异常"。
- 详情失败率过高:
total_fail / (total_success + total_fail) > 0.5 → 飞书/通知 "失败率过高,可能触发反爬"。
# main() 示例结构
def main():
crawler = XxxCrawler(max_workers=3)
total_success, total_fail = crawler.run()
if total_success == 0:
notify("本次执行0条数据入库,请检查网站或API是否异常")
elif total_fail > 0 and total_fail / (total_success + total_fail) > 0.5:
notify("详情抓取失败率过高,可能触发反爬")
if __name__ == "__main__":
try:
main()
except Exception as err:
notify(f"程序报错: {err}")
raise
连续空页中断:
- 列表API连续 N 页(建议3页)返回空结果时,主动中断并打印警告。
- 防止API挂了但程序还在傻翻页浪费时间。
Pagination Stop Logic
翻页停止条件需要区分两种情况,不能混为一谈:
| 情况 |
should_stop |
consecutive_empty |
动作 |
| 本页所有文章早于阈值 |
True |
不累加 |
正常结束(翻到底了) |
| API请求失败/返回空 |
False |
+1 |
交给连续空页计数处理 |
关键原则:
- API 失败时返回
([], False) 而非 ([], True)。
should_stop=True 仅当确认页面有数据但全部早于阈值时才设置。
consecutive_empty 的判断必须在 seen 去重之前,用 raw_tasks(原始列表结果)判断,而非去重后的 tasks。
for page in range(max_pages):
raw_tasks, should_stop = self.parse_list_page(end_index, threshold)
if not raw_tasks:
consecutive_empty += 1
if consecutive_empty >= 3:
print(f"警告:连续{consecutive_empty}页为空,可能API异常")
break
else:
consecutive_empty = 0
tasks = [t for t in raw_tasks if t["id"] not in seen]
seen.update(t["id"] for t in tasks)
tasks = self.filter_not_exists(tasks)
# ... process tasks ...
if should_stop:
break
Proxy & Network
- 不是所有网站都需要代理。海外网站(如 CNBC、Reuters)从香港/海外服务器直连即可,国内代理反而会被拦截(如
449 Foreign Host Forbidden)。
- 当环境变量配置了默认代理时,对不需要代理的站点必须显式传
proxies={} 绕过。
- 在实现前先测试目标站点是否需要代理、是否有 TLS 指纹检测,不要假设。
# 需要绕过默认代理的请求
new_request("GET", url, "json", proxies={}, headers=headers)
Memory Management
- task 列表中只存入库和详情解析所需的字段,不要把 API 原始完整 JSON 塞进 task dict。
raw_data 字段入库时从 item 构建即可,不需要提前存储完整 API 响应。
- 大批量场景下(单次数百条 task),避免在内存中积累所有页的 tasks 后才开始处理,应逐页处理+入库。
Verification Checklist
Do not stop at syntax/lint checks. Verify:
- Table structures and unique keys match the model.
- One sample writes to both main and content tables.
- JSON fields are valid.
- Chinese/Unicode encoding is correct.
summary is a real article summary.
original_source is the true article source.
- Re-running skips successful rows.
- Failed rows can be retried.
- Run the script end-to-end (list → detail → parse) before delivering. Syntax checks alone are insufficient.
- Alerting works: 确认零数据/高失败率/崩溃三种场景都能触发通知。
- Proxy correctness: 确认目标站点请求不被代理拦截(海外站传
proxies={})。
Recommended Class Responsibilities
Prefer a class-based crawler with these responsibilities:
- column configuration
- list/API request
- list field mapping
- per-column threshold calculation
- batched dedupe
- detail parsing
- failed item construction
- transactional main/content upsert
- page-by-page column runner
1---2name: news-crawler-engineering3description: Design, implement, review, and harden news/information crawlers as production data pipelines. Use when the user asks to build, analyze, debug, or refactor news website crawlers, market news crawlers, public opinion/news scraping, article list/detail extraction, pagination, incremental backfill, anti-bot analysis, or crawler database schemas.4metadata:5 source: https://skillmd.com/skills/jinhong270/news-crawler-engineering6---78# News Crawler Engineering910Use this skill whenever working on news/information crawlers. Treat each site as one source in a long-running news collection system, not as a one-off page parser.1112## Workflow13141. Recon first:15 - Find the real list API, pagination params, column/channel IDs, detail URL rule.16 - Check whether list API returns title, timestamp, real source, images, summary, and full content.17 - Inspect page source and JS for `ajax`, `fetch`, `page`, `pageNum`, `columnId`, `channel`.18 - Use browser Network and `user-js-reverse` MCP when JS/signature/cookie logic is involved.19 - Do not implement before confirming pagination and the real data entry point.20212. Pick collection mode:22 - API includes full content: use API as primary source; fetch detail only to enrich missing fields.23 - API is list-only: use API for list/pagination, detail page for content.24 - Static HTML only: parse paginated HTML + details.25 - Strong CSR/anti-bot: reverse API/signature first, then replay.26273. Design data model before coding:28 - Use `get_market_news_info` as the lightweight main table.29 - Use `get_market_news_content` for `content`, `content_html`, `images`, and `raw_data`.30 - Both tables use the business unique key:31 ```sql32 UNIQUE KEY `uk_source_unique_id` (`source`,`source_unique_id`)33 ```34 - `news_id` is only a relation/index, not the content table unique key.35 - Keep large fields out of the main table.3637## Field Rules3839- `media_name`: collection site name.40- `original_source`: true source displayed by the article, empty string if unavailable.41- `summary`: article-specific summary only; never store fixed site description, column intro, or template text.42- `author`: do not use `editor` as a fake author.43- `stock_list`: 文章关联的个股代码列表,格式为纯字符串数组 `["AAPL", "INTC"]`。页面或接口有就提取,没有就存空数组 `[]`。不要存对象数组,不要附带URL等额外信息。44- `crawl_error`: short error only; do not store full HTML/response bodies.45- Site-specific lightweight fields go to `extra_data`; large/raw fields go to the content table.46- Content success rule:47 - Text content exists: success.48 - No text but `images` is non-empty: success; keep `content` empty and store `content_html` if available.49 - No text and no images: `crawl_status = 4` / body empty.5051## Content Extraction5253Prefer structured API data over HTML parsing for content extraction.5455**Data source priority:**56571. API blocks/paragraphs/segments (most reliable, exact paragraph boundaries).582. HTML detail page with known content container selector (fallback).593. Full-page `get_text()` split by newline (last resort).6061**Paragraph rules:**6263- Separate paragraphs with `\n\n` (double newline), not single `\n`.64- When using API blocks, each text block = one paragraph; concatenate inline tokens within a block.65- If the API blocks omit the article intro/teaser (common pattern), prepend `description` as the first paragraph.66- When falling back to HTML: prefer `<p>` tags; also consider `<h2>`–`<h4>`, `<blockquote>`, `<li>` as paragraph-level elements. Skip non-content areas (disclaimers, author bios, related articles) by scoping to the correct content container first.67- If no `<p>` tags exist, fall back to splitting `get_text("\n")` and filtering empty lines.6869**Image position in content:**7071- When the source provides block-structured data with interleaved text and image blocks, insert a placeholder `[图片:caption]` in `content` at the image's position to preserve the spatial relationship between text and images.72- `images` field stores the full URL list (ordered by appearance).73- `content_html` preserves the original HTML structure for re-rendering if needed.7475**Why this matters:**7677- Modern sites (React/Next.js SSR) often return incomplete HTML (images rendered by JS, content behind hydration). The API is the source of truth.78- HTTP headers cannot contain non-ASCII characters. When the detail page URL or Referer contains Chinese/Unicode, URL-encode it (`urllib.parse.quote`).7980## Incremental and Backfill8182News crawlers must support first-time backfill, daily incremental collection, failed retry, batched dedupe, and resumability.8384Standard strategy:85861. Query `MAX(publish_time)` per `source + column_name`.872. If no history exists, threshold is `now - backfill_days` (default 90 days).883. If history exists, threshold is `max_publish_time - overlap_hours` (default 6 hours).894. Iterate by column and page.905. For each page, batch-dedupe candidates with `IN (...)`.916. Fetch/parse details for that page.927. Immediately upsert that page into DB.938. Stop paging when the whole page is older than threshold.9495Never collect all columns/all historical pages into memory and only insert at the end.9697## Storage and Status9899- Write main and content tables in the same transaction.100- Use `INSERT ... ON DUPLICATE KEY UPDATE`.101- Failed records should also be stored for retry and diagnosis.102103`crawl_status`:104105- `1`: success106- `2`: detail request failed107- `3`: page structure abnormal108- `4`: body empty109- `5`: parse exception110111## Code Comments112113**File header**: every crawler script must include a module docstring with `@Desc` followed by a `来源url:` line pointing to the target page URL. This makes it easy to locate the crawl target when debugging.114115```python116"""117@Author: you name118@File : 香港01财经快讯.py python3119@Desc : 香港01(www.hk01.com 繁体站)財經快訊抓取120来源url:https://www.hk01.com/channel/396/%E8%B2%A1%E7%B6%93%E5%BF%AB%E8%A8%8A121"""122```123124Add concise comments to non-obvious logic. Do not narrate what code does line-by-line. Focus on:125126- **Method docstrings**: one-line summary of purpose and return value. Example: `"""获取增量抓取的时间阈值:表里有数据则取最新时间-overlap_hours,无数据则回溯backfill_days天"""`127- **Branch intent**: explain *why* a branch exists when the condition alone isn't self-explanatory. Example: `if not self.db_pool: # 本地调试无DB时直接走全量回溯`128- **Business logic**: threshold calculation, stop conditions, dedup strategy. Example: `should_stop = (not page_has_newer) # 本页全部早于阈值则停止翻页`129- **Non-obvious fallbacks**: when code has multiple data source priorities or fallback paths, annotate the intent. Example: `if not content_text: # blocks为空时兜底用HTML提取`130131Do NOT comment:132- Imports, variable assignments, obvious operations.133- What a function call does (the function name should be self-documenting).134135## Monitoring & Alerting136137每个爬虫必须内置监控报警,不能依赖外部监控平台单独配置。138139**三层报警(必须全部实现):**1401411. **程序崩溃**:未捕获异常 → 飞书/通知 "程序报错"。1422. **零数据入库**:`total_success == 0` → 飞书/通知 "0条数据入库,请检查网站或API是否异常"。1433. **详情失败率过高**:`total_fail / (total_success + total_fail) > 0.5` → 飞书/通知 "失败率过高,可能触发反爬"。144145```python146# main() 示例结构147def main():148 crawler = XxxCrawler(max_workers=3)149 total_success, total_fail = crawler.run()150151 if total_success == 0:152 notify("本次执行0条数据入库,请检查网站或API是否异常")153 elif total_fail > 0 and total_fail / (total_success + total_fail) > 0.5:154 notify("详情抓取失败率过高,可能触发反爬")155156if __name__ == "__main__":157 try:158 main()159 except Exception as err:160 notify(f"程序报错: {err}")161 raise162```163164**连续空页中断:**165166- 列表API连续 N 页(建议3页)返回空结果时,主动中断并打印警告。167- 防止API挂了但程序还在傻翻页浪费时间。168169## Pagination Stop Logic170171翻页停止条件需要区分两种情况,不能混为一谈:172173| 情况 | `should_stop` | `consecutive_empty` | 动作 |174|------|:---:|:---:|------|175| 本页所有文章早于阈值 | `True` | 不累加 | 正常结束(翻到底了) |176| API请求失败/返回空 | `False` | +1 | 交给连续空页计数处理 |177178**关键原则:**179180- API 失败时返回 `([], False)` 而非 `([], True)`。181- `should_stop=True` 仅当确认页面有数据但全部早于阈值时才设置。182- `consecutive_empty` 的判断必须在 `seen` 去重之前,用 `raw_tasks`(原始列表结果)判断,而非去重后的 tasks。183184```python185for page in range(max_pages):186 raw_tasks, should_stop = self.parse_list_page(end_index, threshold)187188 if not raw_tasks:189 consecutive_empty += 1190 if consecutive_empty >= 3:191 print(f"警告:连续{consecutive_empty}页为空,可能API异常")192 break193 else:194 consecutive_empty = 0195196 tasks = [t for t in raw_tasks if t["id"] not in seen]197 seen.update(t["id"] for t in tasks)198 tasks = self.filter_not_exists(tasks)199 # ... process tasks ...200201 if should_stop:202 break203```204205## Proxy & Network206207- 不是所有网站都需要代理。海外网站(如 CNBC、Reuters)从香港/海外服务器直连即可,国内代理反而会被拦截(如 `449 Foreign Host Forbidden`)。208- 当环境变量配置了默认代理时,对不需要代理的站点必须显式传 `proxies={}` 绕过。209- 在实现前先测试目标站点是否需要代理、是否有 TLS 指纹检测,不要假设。210211```python212# 需要绕过默认代理的请求213new_request("GET", url, "json", proxies={}, headers=headers)214```215216## Memory Management217218- task 列表中只存入库和详情解析所需的字段,不要把 API 原始完整 JSON 塞进 task dict。219- `raw_data` 字段入库时从 `item` 构建即可,不需要提前存储完整 API 响应。220- 大批量场景下(单次数百条 task),避免在内存中积累所有页的 tasks 后才开始处理,应逐页处理+入库。221222## Verification Checklist223224Do not stop at syntax/lint checks. Verify:225226- Table structures and unique keys match the model.227- One sample writes to both main and content tables.228- JSON fields are valid.229- Chinese/Unicode encoding is correct.230- `summary` is a real article summary.231- `original_source` is the true article source.232- Re-running skips successful rows.233- Failed rows can be retried.234- **Run the script** end-to-end (list → detail → parse) before delivering. Syntax checks alone are insufficient.235- **Alerting works**: 确认零数据/高失败率/崩溃三种场景都能触发通知。236- **Proxy correctness**: 确认目标站点请求不被代理拦截(海外站传 `proxies={}`)。237238## Recommended Class Responsibilities239240Prefer a class-based crawler with these responsibilities:241242- column configuration243- list/API request244- list field mapping245- per-column threshold calculation246- batched dedupe247- detail parsing248- failed item construction249- transactional main/content upsert250- page-by-page column runner