在asp.net站点中处理会话cookie或302的Scrapy

问题描述 投票:1回答:1

我正在尝试抓取用asp.net编写的Web应用程序。

我正在尝试执行搜索并抓取搜索结果页面。让我们说搜索页面是http://search.site.com/search/search.aspx

现在我的爬虫非常直接

class SitesearchSpider(Spider):
    name = 'sitecrawl'
    allowed_domains = ['search.site.org']
    start_urls = [
        "http://search.site.org/Search/Search.aspx"
    ]

def parse(self, response):
        self.log("Calling Parse Method", level=log.INFO)
        response = response.replace(body=response.body.replace("disabled",""))
        return [FormRequest(
            url="http://search.site.org/Search/Search.aspx",
            formdata={'ctl00$phContent$ucUnifiedSearch$txtIndvl': '2441386'},            
            callback=self.after_search)]

    def after_search(self, response):
        self.log("In after search", level=log.INFO)
        if "To begin your search" in response.body:
            self.log("unable to get result")            
        else:
            self.log(response.body)

但无论同一页面(search.aspx)仅返回到after_search回调而不是预期的searchresults.aspx与结果

这似乎是在浏览器中发生的事情

  1. 搜索词在其中一个字段中输入
  2. 单击搜索按钮
  3. 在表单提交到同一页面(search.aspx)时,我看到它返回302重定向到搜索结果页面
  4. 然后显示搜索结果页面
  5. 我看到这里使用的是asp.net会话cookie,因为一旦进行搜索,我可以将搜索结果页面的URL与http://search.site.com/search/searchresults.aspx?key=searchkey&anothersearchparam=12类似,并打开任何标签页并直接加载结果
  6. 如果我打开一个新会话并粘贴该URL,那么我将被重定向到搜索页面

现在我浏览了文档,我不确定是否必须处理302或aspnet会话cookie。任何帮助,将不胜感激。

python python-2.7 web-scraping scrapy
1个回答
2
投票
  1. 你不必处理302,scrapy本身也是如此。
  2. 您可以调试cookie,在设置上设置DEBUG_COOKIE = 1
  3. 当您从浏览器中搜索时,您是否检查过post或get方法中发送的其他参数,您必须将它们全部传递给表单数据。

我建议你使用fron _response,比如:

return [FormRequest.from_response(
        response,
        formdata={'ctl00$phContent$ucUnifiedSearch$txtIndvl': '2441386'},  
© www.soinside.com 2019 - 2024. All rights reserved.