1.首先先来看初始的代码

import urllib.request
url = "http://jandan.net/ooxx"
response = urllib.request.urlopen(url)
html = response.read().decode("utf-8")
print(html)

这里直接是给urlopen()传入网址,没有经过任何的隐藏所以报错
在这里插入图片描述
因为现在大部分的网站都是有反爬的,会给你识别到是不是用户访问页面的
这里就需要访问的时候添加headers属性
更改后代码为:

import urllib.request
url = "http://jandan.net/ooxx"
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400'}
url = urllib.request.Request(url,headers = headers)
response = urllib.request.urlopen(url)
html = response.read().decode("utf-8")
print(html)

运行结果:
在这里插入图片描述
完美解决
不知道he’aders属性如何获取的,来看我另一篇文章吧:
——>获取headers方法<——

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐