2016-02-02 104 views
1

MySpider试图描述加载更多操作点击,导致网页上的更多项加载更新。这一直持续到没有更多需要加载。Scrapy:POST请求返回JSON响应(200 OK),但数据不完整

yield FormRequest(url,headers=header,formdata={'entity_id': '70431','profile_action': 'review-top','page':str(p), 'limit': '5'},callback=self.parse_review) 

header = {#'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0', 
       'X-Requested-With': 'XMLHttpRequest', 
       'Host': 'www.zomato.com', 
       'Accept': '*/*', 
       'Referer': 'https://www.zomato.com', 
       'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', 
       'dont_filter':'True' } 

url = 'https://www.zomato.com/php/social_load_more.php' 

收到的响应是json响应。

jsonresponse = json.load(response) 

,我也看到 -

('data==', {u'status': u'success', u'left_count': 0, u'html': u"<script type='text/javascript'>if (typeof initiateLaziness == 'function') initiateLaziness() </script>", u'page': u'1', u'more': 0}) 

你看到我得到的地位,left_count,页面,更多的回应。 但是我对'html'感兴趣。不幸的是,它的在纠正值这点我接受,如果通过浏览器完成的(检查网络通话和验证)

预计“HTML”是----

<div><a> very long html stuff...............................................<div><script type='text/javascript'>if (typeof initiateLaziness == 'function') initiateLaziness() </script> 

我只接收稍后部分

<script>...................................</script>. 

真正的html东西丢失。

需要注意的是,我确实收到了回复,但只有'html'不完整。所有好的休息。我相信这可能与动态生成的html有关。但我对此有任何线索。

scrapy中间件没有添加内容长度。也不允许我添加一个。将它添加到标题时,响应失败400。

请求头被实际发送到服务器:

{'Accept-Language': ['en'], 'Accept-Encoding': ['gzip, deflate,br'], 'Dont_Filter': ['True'], 'Connection': ['keep-alive'], 'Accept': ['*/*'], 'User-Agent': ['Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0'], 'Host': ['www.zomato.com'], 'X-Requested-With': ['XMLHttpRequest'], 'Cookie': ['zl=en; fbtrack=9be27330646d24088c56c2531ea2fbf5; fbcity=7; PHPSESSID=2338004ce3fd540477242c3eaee685168163bd05'], 'Referer': ['https://www.zomato.com'], 'Content-Type': ['application/x-www-form-urlencoded; charset=UTF-8']}) 

任何一个可以请帮我,如果我在这里缺少什么? 或者我可以发送发送内容长度/或让中间件发送给我? 非常感谢。

+0

我注意到的一件事是,将“Content-Length:50”添加到标题后,响应失败。而且,中间件不会自动将其添加到请求的标题中。 –

回答

1

由于未使用cookie,您将无法获得html内容作为回应。在你提到的实际请求标题中,有一个cookie属性。但在通过代码发送的ajax请求中,没有cookie字段。

首先在zomato餐厅页面的请求响应中设置一个cookie,其URL为:https://www.zomato.com/city/restaurant/reviews。现在,当点击负载更多按钮时,请求与包含cookie由服务器中的url“https://www.zomato.com/php/social_load_more.php”之前的响应设置cookie字段中发送。因此,每次发出ajax请求时,都会在请求标头中发送前一个响应中设置的cookie,并在当前请求的响应中设置新的cookie。

因此,为了管理这些饼干,我用请求包的会话对象。该脚本也可以在不使用scrapy的情况下编写。当您在scrapy中编写代码时,请查看是否有任何会话对象可用于管理scrapy的cookie。

我的代码:

import requests 
url : 'https://www.zomato.com/city/restaurant/reviews' 
s = requests.Session() 
resp = s.get(url, headers=header) 

上面的代码将请求发送到的餐厅评论的URL。这是非常重要的,因为第一个cookie是在对此请求的响应中设置的。

params={ 
     'entity_id':res_id, 
     'profile_action':'reviews-dd', 
     'page':'1', 
     'limit':'5' 
    } 
header = {"origin":"https://www.zomato.com","Referer":"https://www.zomato.com/","user-agent":"Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0", "x-requested-with":"XMLHttpRequest", 'Accept-Encoding': 'gzip, deflate, br'} 
loadreviews_text = s.post("https://www.zomato.com/php/social_load_more.php", data=params, headers=header) 
loadreviews = loadreviews_text.json() 

现在请求发送到social_load_more.php。该对象的'管理饼干。变量loadreviews现在将具有json格式的html数据。