架构设计:
- Scrapy-Redis做分布式任务调度
- 3台爬虫机器并行采集
- 代理池(自建+第三方)
- MongoDB存储原始数据
- Elasticsearch做全文检索
知乎爬虫核心代码:
code
import scrapy
from scrapy_redis.spiders import RedisSpider
class ZhihuBrandSpider(RedisSpider):
name = 'zhihu_brand'
redis_key = 'zhihu:start_urls'
custom_settings = {'CONCURRENT_REQUESTS': 2,
'RETRY_TIMES': 3,
code
}
def parse(self, response):
# 搜索结果页code
yield {'title': item.css('a.ContentItem-title span::text').get(),
'excerpt': item.css('.RichContent-inner::text').get(''),
'vote_count': item.css('.VoteButton--up::text').get('0'),
'comment_count': item.css('.ContentItem-action button::text').re_first(r'(\d+)'),
'source': 'zhihu',
'crawled_at': datetime.now().isoformat()
code
}
# 翻页
next_page = response.css('.Pagination-next a::attr(href)').get()yield response.follow(next_page, self.parse)
反爬策略:
1. IP轮换:每50个请求换一次代理IP
2. UA随机:维护200个真实浏览器UA的池
3. 请求间隔:3-8秒随机延迟
4. Cookie管理:定期清理并重新获取
5. 验证码处理:接入打码平台
采集覆盖:
- 知乎:搜索+话题+专栏
- 百度百科:公司名条
- 百家号:品牌相关文章
- 微信公众号:通过搜狗入口
- 头条号:品牌相关文章
目前日采集量5000-8000条,运行了4个月没被封过(谨慎设置频率是关键)。
(场景参考:长三角本地企业试点)