python urlOpen使用代理

利用urllib包

import urllib
proxyConfig = 'http://%s:%s@%s' % ('userName', 'password', 'proxy')
inforMation = urllib.urlopen("http://tianya.cn", proxies={'http':proxyConfig})
print inforMation.read()

userName:用户名
password:密码
proxy: 代理加端口
利用urllib2包

import urllib2
proxyConfig = 'http://%s:%s@%s' % ('userName', 'password', 'proxy')
opener = urllib2.build_opener( urllib2.ProxyHandler({'http':proxyConfig}))
urllib2.install_opener(opener)
inforMation = urllib2.urlopen("http://tianya.cn")
print inforMation.read()

eg

import urllib

def getUrlInfo(weburl):
proxyConfig = 'http://%s:%s@%s' % ('sig00459', '256bea3', '129.20.0.156:8080')
inforMation = urllib.urlopen(weburl, proxies={'http':proxyConfig})
return inforMation.read();
if __name__ == "__main__":
print getUrlInfo('http://tianya.cn');

你可能感兴趣的:(原创,Python,Java)