如何更改请求处理?

2024-10-03 02:35:57 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以在Scrapy中更改请求处理? 例如,如果我希望特定的URL不是由scrapys stanard machinery请求的,而是由Selenium请求的,并使我能够使用Selenium驱动程序方法对其进行操作。你知道吗

怎么做?你知道吗


Tags: 方法urlselenium驱动程序scrapymachinerystanardscrapys
2条回答

你想写的是downloader middleware component。你问它是否有可能“改变请求处理”;它的介绍说它是一个“全球改变Scrapy的请求和响应的系统”;我不知道为什么你不会认为这是你要找的,但如果你继续读下去,这正是它听起来的样子。你知道吗

DownloaderMiddleware对象中的关键方法是process_request。正如医生所说:

This method is called for each request that goes through the download middleware.

process_request() should either: return None, return a Response object, return a Request object, or raise IgnoreRequest.

If it returns a Response object, Scrapy won’t bother calling any other process_request() or process_exception() methods, or the appropriate download function; it’ll return that response.

所以,您只需编写一个DownloaderMiddleware,它的process_request调用Selenium,处理它返回的内容,并将其包装在Response中返回。你知道吗

如果不明显,内置的HttpCacheMiddleware应该演示如何做到这一点。你知道吗

您不必更改请求,只需在spider中执行Selenium即可。你知道吗

def parse(self, response):
    browser = webdriver.Firefox()
    for i in response.xpath("//a/@href").extract():
        browser.get(i)
        #then do other stuff

但根据我的经验,只有极少数的事情你不能单独用scrapy来做。 你有一个链接显示什么样的东西,你正在寻找?你知道吗

相关问题 更多 >