春节8部影片集结——《水门桥》、《狙击手》、《奇迹·笨小孩》、《四海》、《这个杀手不太冷静》,《喜羊羊与灰太狼之筐出未来》《熊出没·重返地球》和《小虎墩大英雄》,今天,我们用python爬虫看一下韩寒导演的新作《四海》的评价怎样?
豆瓣评分5.6分:
爬一下评论看看,具体代码如下:
import pandas as pd import requestsimport bs4import timeimport randomdef crawl(url): headers={ 'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9', 'Cookie':'douban-fav-remind=1; gr_user_id=7192bf1b-dff1-4d4e-b442-198662a6e796; bid=bLrpSuzeKFg; _vwo_uuid_v2=DA05DCADC910BEDC1D1D3D0773318CF78|c22091fa3bd072eb5c0220641c8b64d8; ll="118159"; __utma=30149280.361671231.1587465102.1621428340.1643877464.15; __utmc=30149280; __utmz=30149280.1643877464.15.1.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; ap_v=0,6.0; _pk_ref.100001.4cf6=["","",1643877472,"https://www.douban.com/search?q=%E5%9B%9B%E6%B5%B7"]; _pk_ses.100001.4cf6=*; __utma=223695111.48667557.1599446059.1618665220.1643877472.7; __utmb=223695111.0.10.1643877472; __utmc=223695111; __utmz=223695111.1643877472.7.1.utmcsr=douban.com|utmccn=(referral)|utmcmd=referral|utmcct=/search; __gads=ID=6d9ae20d05952062-22b28f7572d000f1:T=1643877472:RT=1643877472:S=ALNI_Maf1NOnNSyULUase9Tc0w3ne8oDGg; __utmt=1; __utmb=30149280.4.10.1643877464; dbcl2="253523217:dPbRGm5Pixk"; ck=SBVj; push_noty_num=0; push_doumail_num=0; _pk_id.100001.4cf6=ae0e18afc045976b.1599446058.7.1643879806.1618665220.' } html=requests.get(url,headers=headers) soup=bs4.BeautifulSoup(html.text) data=[] web_name=soup.select('span.comment-info > a') short_text=soup.select('div.comment > p > span.short') for i,j in zip(web_name,short_text): name=i.get_text() text=j.get_text() #保存一版只带评论的txt文件 with open('<四海>影评.txt','a+') as f: f.write(text) f.close() return(data) for i in range(0,250): #建立一个空的DataFrame inidata=pd.DataFrame() url=f'https://movie.douban.com/subject/35337517/comments?start={i*20}&limit=20&status=P&sort=new_score' print(url) time.sleep(random.random()) crawl(url)
爬取结果为:
做个词云:
呃,词云评论里最多的词句居然是:韩寒、飞驰人生。好吧,果然都是导演、演员光环照耀这部电影,5.6的评分也算中肯,您觉得呢?