python - 有没有办法在 shell 中处理 scrapy.Request 对象?

标签 python scrapy

在终端中,我跑了

scrapy startproject tutorial

我在 spiders 文件夹中创建了以下蜘蛛

import scrapy

class QuotesSpider(scrapy.Spider):
    name = "quotes"
    start_urls = ['http://quotes.toscrape.com/page/1/']

在终端中,我跑了

scrapy shell 'http://quotes.toscrape.com/page/1/'

这一切都工作正常,就像在打开的 Python shell 中一样

>>> response
<200 http://quotes.toscrape.com/page/1/>

现在,我跑了

>>> next_page = response.css('li.next a::attr(href)').extract_first()
>>> next_page
'/page/2/'

>>> response.follow(next_page)
<GET http://quotes.toscrape.com/page/2/>

>>> type(response.follow(next_page))
<class 'scrapy.http.request.Request'>

我想根据 next_page 的链接在 shell 中获取一个新的 Response 对象。这有可能吗?非常感谢任何帮助。

我已经尝试了以下方法,但无法修复错误。

>>> scrapy.downloadermiddlewares.defaultheaders.DefaultHeadersMiddleware.process_request(response.follow(next_page), "quotes")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
TypeError: process_request() missing 1 required positional argument: 'spider'

最佳答案

使用fetch() :

>>> fetch(response.follow(next_page))

关于python - 有没有办法在 shell 中处理 scrapy.Request 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45629370/

相关文章:

python - 打印 'x' 项目数量 'y' 次?

python - 引发类型错误(类型错误 : object of type <class 'numpy.float64' > cannot be safely interpreted as an integer)

python - AWS Cognito 作为网站的 Django 身份验证后端

python - scrapy 检查 url 是否有 404 错误

python - 如何从一个链接生成已解析的项目以及同一项目列表中其他链接的其他已解析的项目

python - scrapy+selenium+phantomjs 中的循环问题

python - 将 ndarray 转换为 cv::Mat 的最简单方法是什么?

python - 答案在控制台中打印但在执行程序文件时不打印(Python)

python - Scrapy - 抓取选定的 div

Scrapy Request方法的meta-args是浅拷贝,而scrapy_redis中Request方法的meta-args是深拷贝。为什么?