python-3.x - 网页抓取到熊猫 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 条独特的评论。
解决方案
我会用
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'}))
推荐阅读
- flutter - 查找当前位置与输入的经纬度之间的距离
- python - 在 pyomo 上找不到求解器“glpk”的可执行文件
- typescript - 泛型的窄容器由泛型
- java - java.io.FileNotFoundException:/storage/emulated/0/Android/data/MyApplication/MyFile.ics。(没有这样的文件或目录)/Android Studio/Ical4j
- javascript - 表单提交后重定向 React Native
- c - 整数数组[10] = {1, 2, 0, 3}。我怎样才能知道这里有 4 个元素?我知道如何找到数组的大小
- php - 如何在laravel中按最高排序结果
- xslt - 如何在 XSLT 中使用 for-each 组对数据编号进行排序
- node.js - Firebase get data by timestamp
- c - 计算机尝试猜测C中的用户号