如何用python抓取预订评论?

时间:2021-05-05 13:18:49

标签: python python-3.x booking.com-api

我想打印特定酒店的所有评论。

我使用此代码:

import urllib.request
from bs4 import BeautifulSoup
url='https://www.booking.com/reviews/co/hotel/ibis-bogota-museo.es.html?page=1;r_lang=all;rows=75'
req = urllib.request.Request(
    url,
    headers={
        'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36',
    }
)

f = urllib.request.urlopen(req)
soup = BeautifulSoup(f.read().decode('utf-8'), 'html.parser')
reviews = soup.findAll("li", {"class": "review_item clearfix "})
for review in reviews:
    print(review.find("div", {"class": "review_item_header_content"}).text)

但没有打印!

请问有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

导入请求 从scrapy导入选择器

url='https://www.booking.com/reviews/co/hotel/ibis-bogota-museo.es.html?page=1;r_lang=all;rows=75'

response2 = requests.get(url)

如果 response2.ok 为真:

selector = Selector(text=response2.content)
print(selector.xpath("//div[@class='review_item_header_content_container']").get())
相关问题