代码:
import urllib from urllib.request import urlopen from urllib.parse import urlencode url='http://xxx.xxxx.com/' //测试链接 res=urlopen(url)#通过urlopen方法访问拼接好的url res=res.read().decode()#read()方法是读取返回数据内容,decode是转换返回数据的bytes格式为str print(res)
错误信息:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:/work/projects/pythonDemo1/HttpTest.py", line 8, in <module>
res=urlopen(url)#通过urlopen方法访问拼接好的url
File "D:\work\soft\Anaconda\envs\py36\lib\urllib\request.py", line 223, in urlopen
return opener.open(url, data, timeout)
File "D:\work\soft\Anaconda\envs\py36\lib\urllib\request.py", line 526, in open
response = self._open(req, data)
File "D:\work\soft\Anaconda\envs\py36\lib\urllib\request.py", line 544, in _open
'_open', req)
File "D:\work\soft\Anaconda\envs\py36\lib\urllib\request.py", line 504, in _call_chain
result = func(*args)
File "D:\work\soft\Anaconda\envs\py36\lib\urllib\request.py", line 1346, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "D:\work\soft\Anaconda\envs\py36\lib\urllib\request.py", line 1320, in do_open
raise URLError(err)
urllib.error.URLError: <urlopen error [WinError 10061] 由于目标计算机积极拒绝,无法连接。>
处理方法:
win10+Python3.6+pycharm+ie
打开ie浏览器-->-->设置-->Interner选项-->弹出Internet属性-->局域网设置-->自动检测设置-->重启pycharm即可