首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BeautifulSoup没有返回空评论,该如何修复

BeautifulSoup没有返回空评论,该如何修复
EN

Stack Overflow用户
提问于 2020-05-26 05:07:48
回答 1查看 27关注 0票数 0
代码语言:javascript
复制
for page in range(0,26): #Remember to update the number of pages 
    url = 'https://www.metacritic.com/game/switch/pokemon-sword/user-reviews?page='+str(page)
    headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 5.1.1; SM-G928X Build/LMY47X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.83 Mobile Safari/537.36'}
    response  = requests.get(url, headers = user_agent)
    #time.sleep(rand.randint(3,30)) 
    soup = BeautifulSoup(response.text, 'html.parser')
    for review in soup.find_all('div', class_='review_content'):
        if review.find('div', class_='name') == None:
                       break 
        review_dict['name'].append(review.find('div', class_='name').find('a').text)
        review_dict['date'].append(review.find('div', class_='date').text)
        review_dict['rating'].append(review.find('div', class_='review_grade').find_all('div')[0].text)
        if review.find('span', class_='blurb blurb_expanded'):
            review_dict['review'].append(review.find('span', class_='blurb blurb_expanded').text)
        else:
            review_dict['review'].append(review.find('div', class_='review_body').find('span').text)

这段代码来自https://towardsdatascience.com/web-scraping-metacritic-reviews-using-beautifulsoup-63801bbe200e

我试图从某个网站获得所有的评论,但我无法获得“评论”来这样做

EN

回答 1

Stack Overflow用户

发布于 2020-05-26 05:26:25

更改用户标头,使其与您链接的示例中的标头匹配。

代码语言:javascript
复制
headers = {'User-agent': 'Mozilla/5.0'}

如果没有它,就不会在页面请求的超文本标记语言中返回review-content类。尝试print(soup),您将看到两个超文本标记语言响应之间的差异。看起来服务器正在根据此指定的标头更改响应。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62010802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档