def askurl(url):
headers = {
"User - Agent": 'Mozilla/5.0(Windows NT 10.0;WOW64) AppleWebKit/537.36(KHTML, likeGecko) Chrome/86.0.4240.198Safari/537.36'
# "User - Agent": 'Mozilla/5.0(Windows NT 10.0;Win64;x64) AppleWebKit/537.36(KHTML, likeGecko) Chrome/99.0.4844.74Safari/537.36'
# "user-agent": 'Mozilla/5.0 (Windows NT 10.0;WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36'
}
req = urllib.request.Request(url=url, headers=headers) # 封装爬虫信息,模拟浏览器
try:
response = urllib.request.urlopen(req) # 获取网站数据
html = response.read().decode('utf-8')
except urllib.error.URLError as e:
if hasattr('e', 'code'): # 返回异常状态码
print(e.code)
if hasattr(e, 'reason'): # 返回异常原因
print(e.reason)
return html # 返回得到的数据
很久之前写的一个爬虫,之前运行也没有问题,今天刚跑了几次之后,然后就跑不起来了,提示一个这样的错误,着实不能理解
UnboundLocalError: local variable 'html' referenced before assignment