尝试对返回大量搜索结果的网站进行一些抓取。问题是导航页面的唯一方法是通过附加到每个链接的 href 参数的 __doPostBack 方法。
这是我当前的代码:
class guruSpider(BaseSpider):
name = "guru"
allowed_domains = ["guru.com"]
def start_requests(self):
baseUrl = "http://www.guru.com/emp/search.aspx?keyword=#&&sort=Earnings&page="
endUrl = "&sort=Earnings"
target = 'ctl00$empcnt$ucResults$pagination'
hiddenValue = ";;AjaxControlToolkit, Version=3.5.60919.0, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e:en-US:311bfa99-1d92-41d7-aace-73bb01d14bf6:475a4ef5:effe2a26:1d3ed089:5546a2b:497ef277:a43b07eb:d2e10b12:37e2e5c9:751cdd15:dfad98a5:3cf12cf1"
requests = []
for i in range(1, 5):
url = baseUrl + str(i)
argument = str(i+1)
data = {'__EVENTTARGET': target, '__EVENTARGUMENT': argument,
'__ASYNCPOST': 'true', 'ctl00_scriptMgr_HiddenField':hiddenValue}
currentPage = FormRequest(url, formdata=data)
requests.append(currentPage)
return requests
这具有以下示例响应:
Crawled (404) <POST http://www.guru.com/emp/search.aspx?keyword=#&&sort=Earnings&page=3> (referer: None)
Crawled (200) <POST http://www.guru.com/emp/search.aspx?keyword=#&&sort=Earnings&page=4> (referer: None)
但是,在 200 消息中,实际返回如下:
<HtmlXPathSelector xpath=None data=u'<html><body><p>179|error|500|The page is'>
有想法该怎么解决这个吗?我真的对此束手无策。