提高scrapy的抓取效率
- 增加并发
- 默认scrapy开启的并发线程的个数是32个,可以适当的进行增加。在settings中进行设置CONCURRENT_REQUESTS=100
- 降低日志级别
- 在运行的时候,会有大量的日志信息的输出,为了减少CPU的使用率。可以设置log输出的信息是INFO或者是ERROR就可以了LOG_LEVEL=‘INFO’
- 禁止cookie
- 如果不是真的需要cookie的话,可以在scrapy爬取的时候禁止cookie从而来减少CPU的使用率,提升爬取的效率,COOKIES_ENABLED=False
- 禁止重试
- 对失败的HTTP进行重新的请求(重试)会减慢爬取的速度,因此可以禁止重试RETRY_ENABLED=False
- 减少下载超时
- 如果对一个非常慢的链接进行爬取,减少下载超时可以能让卡住的链接快速的被放弃,从而来提升效率DOWNLOAD_TIMEOUT=10
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36' # Obey robots.txt rules ROBOTSTXT_OBEY = False # Configure maximum concurrent requests performed by Scrapy (default: 16) CONCURRENT_REQUESTS = 100 COOKIES_ENABLED = False LOG_LEVEL = 'ERROR' RETRY_ENABLED = False DOWNLOAD_TIMEOUT = 3 # Configure a delay for requests for the same website (default: 0) # See https://doc.scrapy.org/en/latest/topics/settings.html#download-delay # See also autothrottle settings and docs # The download delay setting will honor only one of: #CONCURRENT_REQUESTS_PER_DOMAIN = 16 #CONCURRENT_REQUESTS_PER_IP = 16 DOWNLOAD_DELAY = 3