马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
代码如下:import urllib.request
url = "https://www.whatismyip.com/wp-content/uploads/2017/11/WhatIsMyIP.com_Brand_30.png" #访问这个网站会显示ip地址
proxy_support = urllib.request.ProxyHandler({'http':'125.108.106.64:9000'})
opener = urllib.request.build_opener(proxy_support) #定制
opener.addheaders = [('user-agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36')]
urllib.request.install_opener(opener) #安装
response = urllib.request.urlopen(url) #使用代理ip爬目标网站
html = response.read().decode('utf-8')
print(html)
调试时显示这一步出错response = urllib.request.urlopen(url) #使用代理ip爬目标网站
出错界面如图Traceback (most recent call last):
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\pydevd.py", line 1438, in _exec
pydev_imports.execfile(file, globals, locals) # execute the script
File "D:\办公\pycharm\PyCharm Community Edition 2020.1.1\plugins\python-ce\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "E:/python_pycharm/练习/opener.py", line 8, in <module>
response = urllib.request.urlopen(url) #使用代理ip爬目标网站
File "D:\python3.7\Lib\urllib\request.py", line 222, in urlopen
return opener.open(url, data, timeout)
File "D:\python3.7\Lib\urllib\request.py", line 531, in open
response = meth(req, response)
File "D:\python3.7\Lib\urllib\request.py", line 641, in http_response
'http', request, response, code, msg, hdrs)
File "D:\python3.7\Lib\urllib\request.py", line 569, in error
return self._call_chain(*args)
File "D:\python3.7\Lib\urllib\request.py", line 503, in _call_chain
result = func(*args)
File "D:\python3.7\Lib\urllib\request.py", line 649, in http_error_default
raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 403: Forbidden
请大佬指导一下错误原因是啥
|