如何urllib在Python 3中设置最后一个代理。
urllib
from urllib import request as urlrequest ask = urlrequest.Request(url) # note that here Request has R not r as prev versions open = urlrequest.urlopen(req) open.read()
我尝试添加代理,如下所示:
ask=urlrequest.Request.set_proxy(ask,proxies,'http')
但是,由于出现下一个错误,我不知道它的正确性:
336 def set_proxy(self, host, type): --> 337 if self.type == 'https' and not self._tunnel_host: 338 self._tunnel_host = self.host 339 else: AttributeError: 'NoneType' object has no attribute 'type'
您应该在调用classset_proxy()的 实例Request,而不是调用类本身:
set_proxy()
Request
from urllib import request as urlrequest proxy_host = 'localhost:1234' # host and port of your proxy url = 'http://www.httpbin.org/ip' req = urlrequest.Request(url) req.set_proxy(proxy_host, 'http') response = urlrequest.urlopen(req) print(response.read().decode('utf8'))