【问题标题】:Python BeautifulSoup web-scraping Tripadvisor view a reviewPython BeautifulSoup 网页抓取 Tripadvisor 查看评论
【发布时间】:2021-11-12 17:36:43
【问题描述】:

所以我不熟悉网络抓取并尝试查看特定酒店的评论列表。 我最初试图通过选择特定类来查看特定评论,但我没有得到任何输出,即使我尝试检查请求的状态代码,我也没有得到任何输出。我相信我的代码需要很长时间才能运行。

网络抓取需要时间运行还是我的代码有问题?

import requests
from bs4 import BeautifulSoup

headers = {
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Methods': 'GET',
    'Access-Control-Allow-Headers': 'Content-Type',
    'Access-Control-Max-Age': '3600',
    'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0'
    }

url = "https://www.tripadvisor.ca/Hotel_Review-g154913-d1587398-Reviews-Le_Germain_Hotel_Calgary-Calgary_Alberta.html"
req = requests.get(url, headers)

print (req.status_code)
soup = BeautifulSoup(req.content, 'html.parser')

review = soup.find_all(class_="XllAv H4 _a").get_text()
print(review)

【问题讨论】:

  • 可能是您的代码运行速度慢
  • @SuraJS 哇,请问有什么方法可以看到你得到的输出,谢谢?
  • Output 多年来,我每年都去日耳曼卡尔加里参加生日周末度假。这是一个美妙的喘息机会:工作人员很亲切,房间很可爱,位置很好
  • @SurajS 所以你建议它的网速?

标签: python web-scraping beautifulsoup python-requests data-analysis


【解决方案1】:

更改了一些 headers keys 和一些 requests 参数 我在.get_text() 上遇到错误,所以替换为其他

import requests
from bs4 import BeautifulSoup

headers = {
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Methods': 'GET',
        'Access-Control-Allow-Headers': 'Content-Type',
        'accept': '*/*',
        'accept-encoding': 'gzip, deflate',
        'accept-language': 'en,mr;q=0.9',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36'}

url = "https://www.tripadvisor.ca/Hotel_Review-g154913-d1587398-Reviews-Le_Germain_Hotel_Calgary-Calgary_Alberta.html"
req = requests.get(url,headers=headers,timeout=5,verify=False)
print (req.status_code)
soup = BeautifulSoup(req.content, 'html.parser')

#review = soup.find_all(class_="XllAv H4 _a").get_text()
#print(review)
for x in soup.body.find_all(class_="XllAv H4 _a"):
    print(x.text)

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 2018-05-31
    • 2019-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多