爬虫-豆瓣影评项目-error
收藏
UnboundLocalError: local variable 'html' referenced before assignment
第一个豆瓣电影的项目,为什么我每次运行都会有这个错误呢,请问哪个大神知道啊啊
0
收藏
请登录后评论
能截个图看看吗/
看看输出信息,是否有这个信息:
403
Forbidden
原因是豆瓣直接给了403错误,导致html没有赋值,进而导致askURL报错:UnboundLocalError: local variable 'html' referenced before assignment
解决的方法是给它一个headers参数:
headers = {
#伪装一个火狐浏览器
"User-Agent":'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
}
def askURL(url):
request = urllib.request.Request(url=url,headers=headers)#发送请求
在输入header之后,还是有问题