我正在尝试抓取用 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
这就是浏览器中发生的情况
- 在其中一个字段中输入搜索词
- 点击了搜索按钮
- 在表单提交到同一页面 (search.aspx) 时,我看到它返回 302 重定向到搜索结果页面
- 然后将显示搜索结果页面
- 我看到这里使用了 asp.net session cookie,因为一旦进行搜索,我就可以获取搜索结果页面 URL,例如 http://search.site.com/search/searchresults.aspx?key=searchkey&anothersearchparam=12并打开任何选项卡并直接加载结果
- 如果我打开一个新 session 并粘贴 URL,那么我将被重定向到搜索页面
现在我浏览了文档,但不确定是否必须处理 302 或 aspnet session cookie。任何帮助,将不胜感激。
最佳答案
- 您不必处理 302,scrapy 会自己处理。
- 您可以调试 cookie,在设置中设置 DEBUG_COOKIE = 1
- 您是否检查过当您从浏览器搜索时在 post 或 get 方法中发送了哪些其他参数,您必须将它们全部以表单数据形式传递。
我建议您使用 fron _response,例如:
return [FormRequest.from_response(
response,
formdata={'ctl00$phContent$ucUnifiedSearch$txtIndvl': '2441386'},
关于python - Scrapy处理asp.net站点中的 session cookie或302,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24265637/