2016-02-02 100 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數據。