【发布时间】:2020-12-10 10:48:23
【问题描述】:
我正在尝试从 Glass door 中提取评论。但是我面临问题。请按照下面的代码进行操作-
import requests
from bs4 import BeautifulSoup
headers = requests.utils.default_headers()
headers.update({
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0',
})
url = requests.get("https://www.glassdoor.co.in/Reviews/The-Wonderful-Company-Reviews-E1005987.htm?sort.sortType=RD&sort.ascending=false&countryRedirect=true", headers=headers)
urlContent =BeautifulSoup(url.content,"lxml")
print(urlContent)
review = urlContent.find_all('a',class_='reviewLink')
review
title = []
for i in range(0,len(review)):
title.append(review[i].get_text())
title
rating= urlContent.find_all('div',class_='v2__EIReviewsRatingsStylesV2__ratingNum v2__EIReviewsRatingsStylesV2__small')
score=[]
for i in range(0,len(rating)):
score.append(rating[i].get_text())
rev_pros=urlContent.find_all("span",{"data-test":"pros"})
pros=[]
for i in range(0,len(rev_pros)):
pros.append(rev_pros[i].get_text())
pros
rev_cons=urlContent.find_all("span",{"data-test":"cons"})
cons=[]
for i in range(0,len(rev_cons)):
cons.append(rev_cons[i].get_text())
cons
advse=urlContent.find_all("span",{"data-test":"advice-management"})
advse
advise=[]
for i in range(0,len(advse)):
advise.append(advse[i].get_text())
advise
location=urlContent.find_all('span',class_='authorLocation')
location
job_location=[]
for i in range(0,len(location)):
job_location.append(location[i].get_text())
job_location
import pandas as pd
df=pd.DataFrame()
df['Review Title']=title
df['Overall Score']=score
df['Pros']=pros
df['Cons']=cons
df['Jobs_Location']=job_location
df['Advise to Mgmt']=advise
在这里我面临两个挑战-
-
无法为 'advse' 提取任何内容(用于 'Advise to 管理')。
-
当我在数据中使用“工作地点”作为列时出现错误 框架。
(ValueError: Length of values does not match length of index)。 对于这个错误,我的发现是 - 有十行 但是对于“工作地点”的其他列,行数较少 一些评论中未披露位置。
任何机构都可以帮助我解决这个问题。提前致谢。
【问题讨论】:
-
advse 有什么问题?它会返回一个空列表吗? ...至于丢失的位置,似乎 html 在所有 div 中都没有该元素...您的代码正在整个汤中搜索列,尝试按行搜索,如果这有意义的话...找到一个包含所有字段的父类并逐行收集数据
-
是的。 advse 返回空列表
标签: python pandas dataframe web-scraping beautifulsoup