小编典典

将代理设置为urllib.request(Python3)

python

如何urllib在Python 3中设置最后一个代理。

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'

阅读 210

收藏
2021-01-20

共1个答案

小编典典

您应该在调用classset_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'))
2021-01-20