问题在最后。
爬一个网站,遇到下图的cloudflare5秒验证
搜索了一下发现pyspider的github的issues里面已经有人指出了可以使用Anorov/cloudflare-scrape来绕过并且还提交了pull requests:
https://github.com/binux/pyspider/pull/635
我简单测试了一下,发现Anorov/cloudflare-scrape无法绕过我需要爬取的网站获取到内容,但是找到了看起来差不多但略有增强的一个包可以绕过:
https://github.com/VeNoMouS/cloudscraper
主要是通过使用该包的get_tokens方法获取cookies和user_agent传给pyspider来绕过,代码如下:
from pyspider.libs.base_handler import *
import cloudscraper
def getheader(url):
cookie_value, user_agent = cloudscraper.get_tokens(url, browser={'browser': 'chrome', 'mobile': False})
return cookie_value, user_agent
class Handler(BaseHandler):
cookie_value, user_agent = getheader('