Python访问网址时已经添加了user agent还是出现错误
在学习视频教程时,发现自己编写的代码无法成功访问页面,并出现如下错误:urllib.error.HTTPError: HTTP Error 403: Forbidden并且代码中已进添加了user agent,代码如下:
import urllib.request
import os
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36')
response = urllib.request.urlopen(url)
html = response.read()
return html
url = "http://jandan.net/ooxx/MjAyMDA4MzAtOTA=#comments"
url_open(url) import urllib.request
import os
def url_open(url):
req = urllib.request.Request(url)
req.add_header('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.125 Safari/537.36')
response = urllib.request.urlopen(req)
html = response.read()
return html
url = "http://jandan.net/ooxx/MjAyMDA4MzAtOTA=#comments"
print(url_open(url))
修改处在代码第七行,未请求构造好的请求 1q23w31 发表于 2020-8-30 21:00
修改处在代码第七行,未请求构造好的请求
谢了哈哈哈
页:
[1]