查看抓取表格tripadvisor

时间:2017-01-04 12:33:38

标签: python python-3.x web-scraping beautifulsoup tripadvisor

我是python3中的网页抓取新手。我想抓住迪拜所有酒店的评论,但问题是我只能抓住我在网址中描述的酒店评论。任何人都可以告诉我如何在不隐含地给出每个酒店的网址的情况下获得所有酒店评论?

import requests
from bs4 import BeautifulSoup


importurl = 'https://www.tripadvisor.com/Hotel_Review-g295424-d302778-Reviews-Roda_Al_Bustan_Dubai_Airport-Dubai_Emirate_of_Dubai.html'
r = requests.get(importurl)
soup = BeautifulSoup(r.content, "lxml")
 resultsoup = soup.find_all("p", {"class" : "partial_entry"})
#save the reviews to a test text file locally
for review in resultsoup:
review_list = review.get_text()
print(review_list)
with open('testreview.txt', 'w') as fid: 
    for review in resultsoup:
        review_list = review.get_text()
        fid.write(review_list)

1 个答案:

答案 0 :(得分:3)

你应该找到所有酒店的索引页面,将所有链接都列入一个列表,而不是循环网址列表来获取评论。

import bs4, requests
index_pages = ('http://www.tripadvisor.cn/Hotels-g295424-oa{}-Dubai_Emirate_of_Dubai-Hotels.html#ACCOM_OVERVIEW'.format(i) for i in range(0, 540, 30))
urls = []
with requests.session() as s:
    for index in index_pages:
        r = s.get(index)
        soup = bs4.BeautifulSoup(r.text, 'lxml')
        url_list = [i.get('href') for i in soup.select('.property_title')]
        urls.append(url_list)

出:

len(urls): 540