def askURL(url):
head = { #模拟浏览器头部信息,向招聘网服务器发送消息
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
#用户代理,表示告诉招聘网服务器,我们是什么类型的机器、浏览器(本质上是告诉浏览器,我们可以接受什么水平的信息)
request = urllib.request.Request(url=url,headers=head)
html = ""
try:
response = urllib.request.urlopen(request)
html = response.read().decode("gbk")
except urllib.error.URLError as e:
if hasattr(e,"code"):
print(e.code)
if hasattr(e,"reason"):
print(e.reason)
return html
这个是函数,html = response.read().decode("gbk") 这个语句每次都报错
baseurl="https://jobs.51job.com/p"
for i in range(1,3): #调用获取页面信息的函数两次
url = baseurl + str(i)
html = askURL(url) #保存获取到的网页