老师,我抓取电影《楚门的世界》评论的时候,这里一直报错是怎么回事,网上查也查不到?

老师,我抓取电影《楚门的世界》评论的时候,这里一直报错是怎么回事,网上查也查不到?

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
提问者 梦飞_fly 2020-07-24 17:23:18

#爬取评论,我只爬取了一页

import requests
from bs4 import BeautifulSoup

url_duanping = "https://movie.douban.com/subject/1292064/comments?status=P"
headers = {'user-agent':'my-app/0.0.1'}
request1 = requests.get(url=url_duanping,headers = headers)

soup = BeautifulSoup(request1.text,'html.parser')
#print(soup.title.text)
div_comment = soup.find_all(class_ = "comment-item")
print(div_comment[0])

for com in div_comment:
   username = com.find(class_='avatar').a['title']
   comment_time = com.find(class_='comment-time')['title']
   votes = com.find(class_='votes').get_text()
   comment = com.p.get_text()
   with open('1.txt', 'a', encoding='utf8') as file:
       file.write('评论人:' + username + '\n')
       file.write('评论时间:' + comment_time + '\n')
       file.write('支持人数:' + votes + '\n')
       file.write('评论内容:' + comment + '\n')




提问者 梦飞_fly 2020-07-24 16:50:08

哎 !我找了半天,看到别人写的,是class后面加了一个下划线就可以了:”class_“ 不知道为什么要这样,是区分python中的class类吗?

  • DeltaF #1
    对的 class是python关键字,用下划线表示区分,后面应该会讲到,你可能比较超前。以后如果问题我们回答,可以加下qq群里at我一下~
    2020-07-24 22:44:51
  • DeltaF #2
    对的 class是python关键字,用下划线表示区分,后面应该会讲到,你可能比较超前。以后如果问题我们回答,可以加下qq群里at我一下~
    2020-07-24 22:44:53
  • DeltaF #3
    ※ 如果没回答
    2020-07-24 22:45:33
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师