使用 BeautifulSoup 访问 Yelp 评论页面的“aria-label”

问题描述 投票:0回答:2
python web-scraping yelp
2个回答
2
投票

这里

aria-label
不是一个类,它是div标签的一个属性,所以你需要访问它。

from bs4 import BeautifulSoup
s = """<div class="lemon--div__373c0__1mboc i-stars__373c0__1T6rz i-stars--regular-5__373c0__N5JxY border-color--default__373c0__3-ifU overflow--hidden__373c0__2y4YK" aria-label="5 star rating" role="img"><img class="lemon--img__373c0__3GQUb offscreen__373c0__1KofL" src="https://s3-media0.fl.yelpcdn.com/assets/public/stars_v2.yji-52d3d7a328db670d4402843cbddeed89.png" width="132" height="560" alt=""></div>
"""
soup = BeautifulSoup(s, "html.parser")
print(soup.div["aria-label"])

0
投票

作为解析 Yelp 的替代方法,SerpApi 提供了一个Yelp Reviews API。它将绕过块,您不必从头开始创建解析器并进行维护。

简单代码示例:

from serpapi import GoogleSearch
import json

params = {
    'api_key': '...',                       # https://serpapi.com/manage-api-key
    'engine': 'yelp_reviews',               # SerpApi search engine 
    'place_id': 'MGzro82Fi4LYvc86acoONQ',   # Yelp ID of a place
}

search = GoogleSearch(params)               # data extraction on the backend
results = search.get_dict()                 # JSON -> Python dict

# [0] - first organic result from the list of results
rating = results[0]['reviews']['rating']

print(rating)

输出:

5

有一篇使用 SerpApi 和 Python 抓取 Yelp 评论结果 博客文章,其中包含有关如何从 Yelp 评论页面提取所有数据的更多详细信息。

免责声明,我为 SerpApi 工作。

© www.soinside.com 2019 - 2024. All rights reserved.