Python爬取豆瓣的影评并生产词云

 爬取评论的代码如下所示

from  urllib import request
from bs4  import BeautifulSoup  as bs
import os

i= 1
while(i<6):
    i+=1
    url = 'https://book.douban.com/subject/26904658/comments/hot?p='+str(i)
content = request.urlopen(url)
    
html = content.read().decode('utf-8')
soup = bs(html,'html.parser')

comment_div_lists = soup.find_all('span',class_='short')
#print(comment_div_lists)
file = open('pinglun2.txt','w')  #该条命令是如果文件存在就打开,如果不存在会自动创建一个txt文件
for comment in comment_div_lists:
    print(comment.text)
    
    file.write(comment.text)

file.close()



    

生成词云的代码如下

import wordcloud
import jieba
from scipy.misc import imread

mask = imread("china.jpg")

txt = open("pinglun2.txt","r",encoding="utf-8")
t = txt.read()
txt.close()
ls = jieba.lcut(t)
print(len(ls))
tt = " ".join(ls)

w = wordcloud.WordCloud(font_path="simsun.ttc" ,mask =mask,width=1000,height=1000,background_color="white")
w.generate(tt)
w.to_file("pinglun2.jpg")
print("成功")

 

你可能感兴趣的:(Python)