scrapy - 爬取 405
问题描述
start_urls = ['https://www.qichacha.com/search?key=北京证大向上']
def parse(self, response):
# the start_url is a list page, the company_url is a detail_url from the list page
yield scrapy.Request(url=company_url, meta={"infos":info},callback=self.parse_basic_info, dont_filter=True)
当请求 company_url 时,响应 405,但是,如果我使用
response = requests.get(company_url, headers=headers)
print(response.code)
print(response.txt)
然后响应 200 并可以解析 html 页面,或者
start_urls=[company_url]
def parse(self, response):
print(response.code)
print(response.txt)
还有响应200,我不知道为什么响应405时响应405,我打印这样的请求:{'_encoding':'utf-8','method':'GET','_url':' https: //www.qichacha.com/firm_b18bf42ee07d7961e91a0edaf1649287.html ', '_body': b'', 'priority': 0, 'callback': None, 'errback': None, 'cookies': {}, 'headers': {b'User-Agent': [b'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20']}, 'dont_filter': False, '_meta': {'depth': 1}, 'flags': []} 有什么问题?
解决方案
该页面似乎使用默认的用户代理字符串阻止了 Scrapy。像这样运行蜘蛛对我有用:
scrapy runspider -s USER_AGENT="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36" spider.py
或者,您可以USER_AGENT
在项目的settings.py
. 或者,使用类似scrapy-fake-useragent 的东西来自动处理这个问题。
推荐阅读
- reactjs - 使用 Chai\Mocha 设置进行单元测试时,如何让 JSDOM 与 typescript 一起使用?
- azure - 如何使用 Azure Python SDK 预配 Databricks 服务?
- javascript - 有什么办法可以在不连接这两个数组的情况下解决这个问题?
- javascript - 如何使用 js 计算空 div 和正在使用的 div 的数量?
- types - 如何判断一个类型来自哪个模块?
- javascript - 如何在 VUE 项目中导入 API/Script.js 并使用其变量
- ruby-on-rails - 通过 React Native (JWT) 向 Rails API 发送授权
- excel - 如何从未打开的工作簿中获取数据?
- java - java什么时候执行类级别的字段赋值?
- reactjs - 从父组件激活模态,子引用未定义,如何不解除状态?