首页 > 解决方案 > 试图在 vscode 调试控制台中执行一个scrapy FormResponse

问题描述

我正在尝试学习如何使用 python scrapy,我想知道是否可以从 vscode 调试控制台手动请求网站。通常我会使用 python 请求和 BeautifulSoup 来获取网站 html,然后我会输入类似

resp = requests.get(website)

直接进入vscode中的调试控制台。从那里我可以在不重新启动调试器的情况下向网站发出更多请求。但是,当我使用scrapy 时,我找不到做同样事情的方法,因为scrapy 请求将被产生/返回。我的请求示例:

yield scrapy.Request(website, callback=self.parse_site})

如果我尝试粘贴scrapy.Request(website ...)到 vscode 调试控制台,我会得到一个 scrapy 请求对象,而不是我需要的响应。我试图在不重新启动调试器的情况下处理请求,每次我想创建一个。有没有办法做到这一点?

标签: pythondebuggingvisual-studio-codescrapy

解决方案


检查scrapy shell的文档。

# Either type:
scrapy shell 'https://www.somewebsite.com'

# or:
scrapy shell

# and then create the request and fetch it:
req = scrapy.Request(url='https://www.somewebsite.com')
fetch(req)

# now just get whatever you want from the response, for example:
response.status
response.xpath('//div//text()')

在代码中间打开scrapy shell:

def parse_func(self, response):
    from scrapy.shell import inspect_response
    inspect_response(response, self)

推荐阅读