2014-11-08 18 views
0

我使用的Web刮如何湊這頁現成BeautifulSoap後加載的內容

我想這刮URL

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

我已經颳了所有需要的PythonBeautifulSoap內容但不能刮Guest Reviews部分。

其實問題是這個部分是由Ajax加載的,http://www.wotif.com/review/fragment?propertyId=W3830&limit=5加載那部分。而這個網址只返回HTML而不是全部評論

當我用BeautifulSoap刮該頁面的HTML時,它沒有Guest Reviews部分。

hotel_url = "http://www.wotif.com/hotel/View?hotel=W3830&page=1&adults=2&startDay=2014-11- 08&region=1&descriptionSearch=true" 
hotel_page = requests.get(hotel_url).text 
hotel_page_soup = BeautifulSoup(hotel_page) 

請問我該怎麼辦?無論我沒有得到所有的客人評論,但我想獲得最初的5個評論,當您在瀏覽器中查看時加載

+0

你獲得許可刮自己的網站? – 2014-11-08 15:35:36

+0

是............. – Umair 2014-11-08 15:37:04

回答

0

主要監督是在假設the "AJAX" request返回HTML:它實際上是JSON(包含,因爲它發生,在關鍵字html下的一個HTML blob)。你也可以在HTTP響應中看到這一點。

試試這個,然後:

import json 
import requests 
from bs4 import BeautifulSoup 

HOTEL_URL = 'http://www.wotif.com/review/fragment?propertyId=W3830&limit=5' 
content = requests.get(HOTEL_URL).text 
html = json.loads(content)["html"] 
soup = BeautifulSoup(html) 
reviews = soup.find_all('li', class_='review') 
# Returns a list of HTML contents for each review. 
# ... or use find_all('ul', 'review-details-comments') for less HTML 
+0

'hotel_url'是另一個..請參閱編輯的問題 – Umair 2014-11-08 15:53:33

+0

請參閱更新的答案。 – declension 2014-11-08 16:23:36

+0

親愛的..看到我的問題中的Hotel_URL ...這是不同的網址...我想'BeautifulSoap'完成它的所有Ajax請求時抓取頁面... – Umair 2014-11-08 16:26:40

相關問題