2014-11-08 61 views
2

我刮:刮頁面完成

http://www.wotif.com/hotel/View?hotel=W3830&page=1&adults=2&startDay=2014-11-08&region=1&descriptionSearch=true#property-reviews 

使用下面的代碼:

hotel_page = requests.get(hotel_url).text 
hotel_page_soup = BeautifulSoup(hotel_page) 

但是,這不包括Guest Review部分,原因是它是裝在加載頁面後通過AJAX調用。

問題:如何在完成所有AJAX調用後才能刮頁面?

+0

是你能解決這個問題?我有同樣的問題 – user1050619 2015-12-01 17:08:30

回答

2

你需要調用這個URL以及確保X-Requested-WithXMLHttpRequest

URL="http://www.wotif.com/review/fragment?propertyId=W3830&limit=5" 

headers={"X-Requested-With":"XMLHttpRequest", 
"User-Agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.125 Safari/537.36"} 

r=requests.get(URL,headers=headers) 

#response here will be in json format 
#Page source can be extracted using key `html'` 
response=r.json()['html'] 
soup=BeautifulSoup(response) 
reviews=soup.find(class_="review-score review-score-large").text 
print reviews 

Out[]:u'\n\n4.4\nOut of 5\n\n\n' 

print reviews.strip() 

Out[]:u'4.4\nOut of 5' 
+1

但是,這將只加載每次'5'客人評論...而且我將不得不把這個在循環得到所有的評論 – Umair 2014-11-09 13:03:25

+0

@Umair那麼這有什麼問題?你也是這樣做的。我回答了你的問題 - 如果你需要更多,請相應地改變你的問題,我會回答它 – 2014-11-09 21:42:04

+0

你颳了錯誤的URL ...在我的問題中看到...我想颳了一個後,它已經AJAX – Umair 2014-11-10 04:52:30

-1

這很簡單。如果您請求URL http://www.wotif.com/review/fragment.json?propertyId=W3830&limit=100&bestThing=True,您將獲得json格式的所有評論。

URL http://www.wotif.com/review/fragment?propertyId=W3830&limit=100&爲您提供嵌入json中的html的評論。你必須看看自己,最適合你的需求。

+0

如果您訪問 'http://www.wotif.com/review/fragment.json?propertyId = W3830&limit = 100&bestThing = True' 您將會看到評論日期和評論分數。 ..但在JSON響應..他們不可用... – Umair 2014-11-09 06:23:33