本项目利用Python编写代码,从豆瓣电影中提取用户评论数据,并运用相关库生成美观的词云图以直观呈现评论中的高频词汇。
# -*-coding:utf-8 -*-
import urllib.request
from bs4 import BeautifulSoup
def getHtml(url):
获取url页面
headers = {
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36
}
req = urllib.request.Request(url, headers=headers)
try:
response = urllib.request.urlopen(req)
html = response.read()
return html
except Exception as e:
print(fAn error occurred: {e})
return None