【发布时间】:2020-07-01 22:03:53
【问题描述】:
我正在尝试使用 Scrapy 抓取页面“https://zhuanlan.zhihu.com/wangzhenotes”,配置为in the post,并在本文结尾。
这个命令
scrapy shell 'https://zhuanlan.zhihu.com/wangzhenotes'
了解我
2020-07-02 05:50:04 [scrapy.core.engine] DEBUG: Crawled (404) <GET https://zhuanlan.zhihu.com/robots.txt> (referer: None)
2020-07-02 05:50:04 [protego] DEBUG: Rule at line 19 without any user agent to enforce it on.
...
2020-07-02 05:50:04 [scrapy.core.engine] DEBUG: Crawled (200) <GET https://zhuanlan.zhihu.com/wangzhenotes> (referer: None)
[s] Available Scrapy objects:
[s] scrapy scrapy module (contains scrapy.Request, scrapy.Selector, etc)
[s] crawler <scrapy.crawler.Crawler object at 0x10ac98790>
[s] item {}
[s] request <GET https://zhuanlan.zhihu.com/wangzhenotes>
[s] response <200 https://zhuanlan.zhihu.com/wangzhenotes>
...
我猜爬虫被阻止了,因为这个命令只有 3,
len(response.xpath('//span'))
虽然在 Chrome 浏览器的源代码中搜索“span”超过 80,
而response.css("h2.ContentItem-title") 得到一个空列表[]。
我如何获得这些跨度?
这是我正在使用的配置,与the referred post中的配置相同。
class CustomMiddleware(object):
def process_request(self, request, spider):
request.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36"
DOWNLOADER_MIDDLEWARES = {
'projectname.middlewares.CustomMiddleware': 543,
}
【问题讨论】:
-
ROBOTSTXT_OBEY = False应该可以解决 404 错误..
标签: scrapy