【发布时间】:2017-09-01 22:15:32
【问题描述】:
我正在实施一个抓取蜘蛛来抓取包含房地产报价的网站。该站点包含房地产经纪人的电话号码,可以通过 ajax 发布请求检索该电话号码。 scrapy 产生的请求从服务器返回错误,而从 Postman 发送的相同请求返回所需的数据。
这是网站网址:https://www.otodom.pl/oferta/piekne-mieszkanie-na-mokotowie-do-wynajecia-ID3ezHA.html
我使用 chrome 开发工具中的网络选项卡记录了请求。 ajax请求的url为:enter link description here发送请求所需的数据是页面源中包含的CSRFtoken,它会周期性的变化。在 Postman 中,仅提供 CSRFtoken 作为表单数据给出了预期的答案。
这就是我在 scrapy 中构造请求的方式:
token_input = response.xpath('//script[contains(./text(), "csrf")]/text()').extract_first()
csrf_token = token_input[23:-4]
offerID_input = response.xpath('//link[@rel="canonical"]/@href').extract_first()
offerID = (offerID_input[:-5])[-7:]
form_data = {'CSRFToken' : csrf_token}
request_to_send = scrapy.Request(url='https://www.otodom.pl/ajax/misc/contact/phone/3ezHA/', headers = {"Content-Type" : "application/x-www-form-urlencoded"}, method="POST", body=urllib.urlencode(form_data), callback = self.get_phone)
yield request_to_send
不幸的是,我收到了一个错误,尽管一切都应该没问题。有谁知道可能是什么问题?是否可能与编码有关?该网站使用 utf-8。
【问题讨论】: