Skip to main content
 首页 » 编程设计

python之Scrapy 立即跟随 302 重定向

2025年05月04日10kuangbin

我在所有链接都相似的 ASP 网站上使用 scrapy:

javascript:__doPostBack('gridID','Select$0') 
javascript:__doPostBack('gridID','Select$1') 
.... 

我可以使用 FormRequest 跟随任何记录的详细信息页面的链接:

    # Let's first grab all of the Details links -- we can get everything from them that we want 
    for sel in response.xpath("//table[@id='gridID']/tr[td]")[0:20]: 
        thisTarget  = sel.xpath("td")[0].xpath("a/@href").extract()[0].split("'")[1] 
        thisArg     = sel.xpath("td")[0].xpath("a/@href").extract()[0].split("'")[3] 
        yield scrapy.FormRequest.from_response(  
                response, 
                formdata={'__EVENTTARGET'   : thisTarget,  
                          '__EVENTARGUMENT' : thisArg, 
                          '__EVENTVALIDATION': response.xpath("//input[@id='__EVENTVALIDATION']/@value").extract()[0], 
                          '__VIEWSTATE': response.xpath("//input[@id='__VIEWSTATE']/@value").extract()[0] 
                         }, 
                dont_click=True,  
                callback=self.parseDetail, 
                dont_filter=True 
            ) 

但是当 scrapy 一次处理多个项目时,它会分批发出请求。一次五行会导致:

2015-02-20 22:41:19-0500 [spider] DEBUG: Redirecting (302) to <GET http://domain.com/ListingDetail.aspx> from <POST http://domain.com/Listing.aspx> 
2015-02-20 22:41:20-0500 [spider] DEBUG: Redirecting (302) to <GET http://domain.com/ListingDetail.aspx> from <POST http://domain.com/Listing.aspx> 
2015-02-20 22:41:20-0500 [spider] DEBUG: Redirecting (302) to <GET http://domain.com/ListingDetail.aspx> from <POST http://domain.com/Listing.aspx> 
2015-02-20 22:41:21-0500 [spider] DEBUG: Redirecting (302) to <GET http://domain.com/ListingDetail.aspx> from <POST http://domain.com/Listing.aspx> 
2015-02-20 22:41:22-0500 [spider] DEBUG: Redirecting (302) to <GET http://domain.com/ListingDetail.aspx> from <POST http://domain.com/Listing.aspx> 
2015-02-20 22:41:22-0500 [spider] DEBUG: Crawled (200) <GET http://domain.com/ListingDetail.aspx> (referer: http://domain.com/Listing.aspx) 
### Callback executed 
2015-02-20 22:41:23-0500 [spider] DEBUG: Crawled (200) <GET http://domain.com/ListingDetail.aspx> (referer: http://domain.com/Listing.aspx) 
### Callback executed 
2015-02-20 22:41:23-0500 [spider] DEBUG: Crawled (200) <GET http://domain.com/ListingDetail.aspx> (referer: http://domain.com/Listing.aspx) 
### Callback executed 
2015-02-20 22:41:24-0500 [spider] DEBUG: Crawled (200) <GET http://domain.com/ListingDetail.aspx> (referer: http://domain.com/Listing.aspx) 
### Callback executed 
2015-02-20 22:41:24-0500 [spider] DEBUG: Crawled (200) <GET http://domain.com/ListingDetail.aspx> (referer: http://domain.com/Listing.aspx) 
### Callback executed 

这似乎导致所有 5 个响应都相同,我想这是某些 ASP 魔术的结果。

我尝试设置 REDIRECT_PRIORITY_ADJUST = 100 以给予重定向更高的优先级,但收效甚微。最好的做法是在 16 个初始请求后停止,并执行 16 个重定向,然后是另一批初始请求,依此类推....

当我在 scrapy shell 中手动执行操作时,通过 fetching 每个 FormRequest,立即处理重定向,我得到预期的响应,即使在连续获取多个请求时也是如此。

因此,我的问题:

有什么方法可以让 scrapy 处理请求一直到 HTTP 200 响应,并立即执行沿途的任何重定向?

或者...我的问题的任何其他可能不明显的解决方案?

请您参考如下方法:

我在使用 FormRequest 时遇到了同样的问题,网站发回 302 重定向。许多请求的响应都是相同的。它似乎是在下载器中间件之前,甚至是在 scrapy 请求和扭曲之间,因为我放置了一个自定义下载器中间件来查看响应和发出的请求。它有同样的问题。

使用以下 scrapy 设置找到了解决方法。

CONCURRENT_REQUESTS=1 
CONCURRENT_REQUESTS_PER_DOMAIN=1 

但应该有更好的方法。