<p>首先将解析方法重命名为:</p>
<pre><code>def start_requests(self):
</code></pre>
<p>发送表单时,您应该使用scrapy.FormRequest请求相反。如果您想要发送一个原始的正文,那么只需要使用method=post。在这种情况下,它看起来像ofrm数据,所以这样做。你知道吗</p>
<pre><code> formdata = {
'requests': [{'indexName': 'listings',
'params': 'query=&hitsPerPage=24&page=0&highlightPreTag=__ais-highlight__&highlightPostTag=__%2Fais-highlight__&filters=announce_type%3Aproperty-announces%20AND%20language_code%3Apt%20AND%20listing_id%3A%205&facets=%5B%22announce_type%22%5D&tagFilters='}]
}
headers = {
'accept': 'application/json',
'content-type': 'application/x-www-form-urlencoded',
'Origin': 'https://www.flat.com.br',
'Referer': 'https://www.flat.com.br/search?query=',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
}
def start_requests(self):
for link in self.start_urls:
yield scrapy.FormRequest(link, headers=headers, formdata=formdata, callback=self.parse_page)
</code></pre>
<p>您还可以使用其他工具,例如来自响应的表单请求,来帮助实现这一点。如果您想发送一个原始的json字符串或其他东西,那么您需要将字典转换为字符串,然后像这里所做的那样将方法设置为POST。FormRequest将自动发送POST请求,如果您使用from response特性,它将是智能的。你知道吗</p>
<p>参考文献:
<a href="https://docs.scrapy.org/en/latest/topics/request-response.html#request-subclasses" rel="nofollow noreferrer">https://docs.scrapy.org/en/latest/topics/request-response.html#request-subclasses</a></p>