python问题发现-python爬虫——使用代理服务器 进行Handler与build_opener

python爬虫——使用代理服务器 进行Handler与build_opener学习

    • 代码如下
    • 构建handler处理器 opener 学习
    • 问题代码

代码如下

import urllib.request

#代理开关,表示是否启动代理
proxyswitch = True
#构建一个Handler处理器独享,参数是一个字典类型,包括代理类型和代理服务器ip+端口
Praxy_handlerr=urllib.request.ProxyHandler({‘http’:‘101.231.234.38:8080’})
#构建一个没有代理的处理器对象
unllPraxy_handler = urllib.request.ProxyHandler({})
if proxyswitch:
opener = urllib.request.build_opener(Praxy_handlerr)
else:
opener = urllib.request.build_opener(unllPraxy_handler)
#构建一个全局的opener,之后所有的请求都可以用urlopen()方式去发送,也附带Handler的功能
urllib.request.install_opener(opener)
request = urllib.request.Request(‘http://www.baidu.com’)
respons = urllib.request.urlopen(request)
print(respons.read().decode(‘utf-8’))

构建handler处理器 opener 学习

问题代码

Traceback (most recent call last):
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 1317, in do_open
encode_chunked=req.has_header(‘Transfer-encoding’))
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1229, in request
self._send_request(method, url, body, headers, encode_chunked)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1275, in _send_request
self.endheaders(body, encode_chunked=encode_chunked)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1224, in endheaders
self._send_output(message_body, encode_chunked=encode_chunked)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 1016, in _send_output
self.send(msg)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 956, in send
self.connect()
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\http\client.py”, line 928, in connect
(self.host,self.port), self.timeout, self.source_address)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\socket.py”, line 707, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\socket.py”, line 748, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File “D:/python/python work/1/venv/Scripts/HandlerandOpener2.py”, line 16, in
respons = urllib.request.urlopen(request)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 222, in urlopen
return opener.open(url, data, timeout)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 525, in open
response = self._open(req, data)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 543, in _open
‘_open’, req)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 503, in _call_chain
result = func(*args)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 1345, in http_open
return self.do_open(http.client.HTTPConnection, req)
File “C:\Users\德鑫\AppData\Local\Programs\Python\Python37-32\lib\urllib\request.py”, line 1319, in do_open
raise URLError(err)
urllib.error.URLError:

问题未解决

你可能感兴趣的:(python爬虫学习)