首页 > 解决方案 > 网页抓取到熊猫 DF

问题描述

在我尝试将网络评论刮入数据框之前,如果有人问过这个问题,请道歉。我遇到的问题是它抓取了 10 次相同的评论,而不是 10 次不同的评论。

''' 来自 bs4 的导入请求 import BeautifulSoup import pandas as pd

网址 = ' https://www.marriott.com/hotels/hotel-reviews/amsnt-amsterdam-marriott-hotel '

for page in range(10):
page = requests.get("https://www.marriott.com/hotels/hotel-reviews/amsnt-amsterdam-marriott-hotel")

soup = BeautifulSoup(page.content, 'html.parser')

general_data = soup.find_all(class_='bvseo-review')
i = 1
first = general_data[i]
i+=1

for item in general_data:
    span = first.find_all('span')
    description = first.find_all('span', attrs={'itemprop':'description'})
    rating = first.find_all('span', attrs={'itemprop':'ratingValue'})
    auteur = first.find_all('span', attrs={'itemprop':'author'})

pagereviews = pd.DataFrame({
    "description":description,
    "ratingValue":rating,
    "author":auteur
})

pagereviews

'''

结果将是 DF 将包含 10 条独特的评论。

标签: python-3.x

解决方案


我会用

span = []
description = []
rating = []
auteur = []
for item in general_data:
    span.append(item.find_all('span'))
    description.append(item.find_all('span', attrs={'itemprop':'description'}))
    rating.append(item.find_all('span', attrs={'itemprop':'ratingValue'}))
    auteur.append(item.find_all('span', attrs={'itemprop':'author'}))


推荐阅读