python - Scrapy 使用 CrawlSpider 不起作用
问题描述
我正在使用 Scrapy,我尝试使用蜘蛛来抓取整个网站,但我的终端没有得到任何结果。
PS:我在脚本中从浏览器运行 Scrapy。
这是我的代码:
import scrapy
from scrapy.crawler import CrawlerProcess
from scrapy.spiders import CrawlSpider, Rule
from scrapy.linkextractors import LinkExtractor
class MySpider(CrawlSpider):
name = 'website.com'
allowed_domains = ['website.com']
start_urls = ['http://www.website.com']
rules = (
# Extract links matching 'category.php' (but not matching 'subsection.php')
# and follow links from them (since no callback means follow=True by default).
Rule(LinkExtractor(allow=('/', ), deny=('subsection\.php', ))),
# Extract links matching 'item.php' and parse them with the spider's method parse_item
)
def parse_item(self, response):
print(response.css('title').extract())
process = CrawlerProcess({
'USER_AGENT': 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1)'
})
process.crawl(MySpider)
process.start()
解决方案
你错过了回调参数。
简单地改变
Rule(LinkExtractor(allow=('/', ), deny=('subsection\.php', ))),
至
Rule(LinkExtractor(allow=('/', ), deny=('subsection\.php', )), callback='parse_item')
根据crawlspider文档,您忘记将回调参数传递给您的链接提取器。
推荐阅读
- c# - 发布对象数组时从 Web API 接收 405 HTTP 响应(.Net Core 5)
- git - 合并冲突:从分支中提取单个文件时
- sql - 缺少一个属性
- android - 何时使用 viewDataBinding.invalidateAll()
- android - Kotlin Flow - callbackFlow 的替代品
- emacs - jedi:goto-definition 跳过导入部分
- vb.net - 我如何验证这一点?
- python - 如何主动删除重复输入到 Flask 服务器上的文本文件中?
- string - Qt:省略富文本
- python - 变量的作用就像 Python 中的指针?