如何将评论抓取到数据框

时间:2021-02-16 10:14:14

标签: python pandas dataframe beautifulsoup

我想从 page 中抓取评论并将它们保存为数据框,但我不下载星级评分和评论文本。只是文字而已。我做错了什么?

import csv
import pandas as pd
import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.morele.net/pralka-candy-cs4-1062d3-950636/?sekcja=reviews-all")

soup = BeautifulSoup(page.content, "html.parser",
).find_all("div", {"class":"reviews-item"}) 
# print(soup)
morele = [div.getText(strip=True) for div in soup]

print(morele)
csv_table = pd.DataFrame(morele)
csv_table = csv_table.reset_index(drop=True)
csv_table.insert(0,'No.',csv_table.index)

1 个答案:

答案 0 :(得分:0)

您大部分时间都在那里 - 只需进一步导航 DOM,您就可以获得文本。

import requests
from bs4 import BeautifulSoup

page = requests.get("https://www.morele.net/pralka-candy-cs4-1062d3-950636/?sekcja=reviews-all")

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

data = [{"text":ri.find("div", {"class":"rev-desc"}).getText(strip=True) ,
        "stars":ri.find("div", {"class":"rev-stars"}).getText(strip=True)}
 for ri in soup.find_all("div", {"class":"reviews-item"})
]

pd.DataFrame(data)