我是编程和 StackOverflow 的完全初学者,我只需要从 TripAdvisor 页面进行一些基本的网络抓取并从中清除一些有用的信息。很好地展示它等等。我试图将咖啡馆的名称、评级数量和评级本身隔离开来。我想我可能需要将它转换为文本并使用正则表达式或其他什么?我真的不知道。我的意思的一个例子是:
输出:
咖啡厅,5 个气泡中的 4 个,201 条评论。
类似的东西。我会把我的代码放在下面,我能得到的任何帮助都会很棒,我将无限感激。干杯。
from bs4 import BeautifulSoup
def get_HTML(url):
response = urllib.request.urlopen(url)
html = response.read()
return html
Tripadvisor_reviews_HTML=get_HTML(
'https://www.tripadvisor.com.au/Restaurants-
g255068-c8-Brisbane_Brisbane_Region_Queensland.html')
def get_review_count(HTML):
soup = BeautifulSoup(Tripadvisor_reviews_HTML, "lxml")
for element in soup(attrs={'class' : 'reviewCount'}):
print(element)
get_review_count(Tripadvisor_reviews_HTML)
def get_review_score(HTML):
soup = BeautifulSoup(Tripadvisor_reviews_HTML, "lxml")
for four_point_five_score in soup(attrs={'alt' : '4.5 of 5 bubbles'}):
print(four_point_five_score)
get_review_score(Tripadvisor_reviews_HTML)
def get_cafe_name(HTML):
soup = BeautifulSoup(Tripadvisor_reviews_HTML, "lxml")
for name in soup(attrs={'class' : "property_title"}):
print(name)
get_cafe_name(Tripadvisor_reviews_HTML)
最佳答案
您忘记在每个打印语句中使用 .text
。但是,请尝试使用以下方法从该站点获取所有三个字段。
from bs4 import BeautifulSoup
import urllib.request
URL = "https://www.tripadvisor.com.au/Restaurants-g255068-c8-Brisbane_Brisbane_Region_Queensland.html"
def get_info(link):
response = urllib.request.urlopen(link)
soup = BeautifulSoup(response.read(),"lxml")
for items in soup.find_all(class_="shortSellDetails"):
name = items.find(class_="property_title").get_text(strip=True)
bubble = items.find(class_="ui_bubble_rating").get("alt")
review = items.find(class_="reviewCount").get_text(strip=True)
print(name,bubble,review)
if __name__ == '__main__':
get_info(URL)
你可能会得到这样的结果:
Double Shot New Farm 4.5 of 5 bubbles 218 reviews
Goodness Gracious Cafe 4.5 of 5 bubbles 150 reviews
New Farm Deli & Cafe 4.5 of 5 bubbles 273 reviews
Coffee Anthology 4.5 of 5 bubbles 116 reviews
关于python - 基本 Python BeautifulSoup 网页抓取 Tripadvisor 评论和数据清理,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50422110/