
使用Python抓取豆瓣电影评论,并进行词云可视化展示。
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
import urllib.request
from bs4 import BeautifulSoup
def getHtml(url):
获取指定URL页面的HTML内容
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, timeout=30) # 添加超时处理,防止程序卡死
html = response.read().decode(utf-8) # 明确指定编码方式,避免乱码问题
return html
except Exception as e:
print(获取页面失败,错误信息:, str(e)) # 打印错误信息方便调试
return None
全部评论 (0)
还没有任何评论哟~


