Python 2.6 -> Python 3 (ProxyHandler)

5
我写了一个可以使用代理的脚本(py2.6x):
proxy_support = urllib2.ProxyHandler({'http' : 'http://127.0.0.1:80'})

但在py3.11x中,没有urllib2,只有urllib...而且它不支持ProxyHandler。

我该如何使用代理来使用urllib呢?Python 3不是比Python 2更新吗?为什么在新版本中删除了urllib2?

2个回答

10

在Python 3中,urllib2.ProxyHandler现在是urllib.request.ProxyHandler

import urllib.request
proxy_support = urllib.request.ProxyHandler({'http' : 'http://127.0.0.1:80'})

许多旧的url*库已经与urllib包合并。 此处有一个很好的解释。


3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接