从 yelp - beautifulsoup 中提取评论

Pulling reviews from yelp - beautifulsoup

所以我想从 yelp 获取酒店的所有评论:https://www.yelp.com/biz/capri-laguna-laguna-beach

我的代码如下,但我无法提取所有评论。我只能提取一个。有人可以帮忙吗?

理想情况下,我很乐意为该机构提取所有 yelp 评论

import time
import random
from bs4 import BeautifulSoup as bs

import urllib.request as url


html = urllib.request.urlopen('https://www.yelp.com/biz/capri-laguna-laguna-beach').read().decode('utf-8')

soup = bs(html, 'html.parser')

relevant= soup.find_all('p', class_='comment__09f24__gu0rG css-qgunke')

for div in relevant:
        for html_class in div.find_all('span',class_="raw__09f24__T4Ezm"):
            text = html_class.find('span')
            review = html_class.getText(
            
print(review)

您只打印了一篇评论,因为您在循环外编写了打印语句。

relevant= soup.find_all('p', class_='comment__09f24__gu0rG css-qgunke')

for div in relevant:
  for html_class in div.find_all('span',class_="raw__09f24__T4Ezm"):
    text = html_class.find('span')
    review = html_class.getText()          
    print(review)

如果您执行上面的代码,您将打印所有 10 条评论。


要将所有 10 条评论存储在一个列表中,请执行此操作,

reviews = []

for div in relevant:
  for html_class in div.find_all('span',class_="raw__09f24__T4Ezm"):
    text = html_class.find('span')
    review = html_class.getText()
    reviews.append(review)