酷符网动态语言 > 正文

python使用urllib代理上网

2011-01-29 12:38codeif.com

大家可以参考这篇文章:http://docs.python.org/library/urllib.html

代理上网设置如下

# Use http://www.someproxy.com:3128 for http proxying
proxies = {'http': 'http://www.someproxy.com:3128'}
filehandle = urllib.urlopen(some_url, proxies=proxies)
# Don't use any proxies
filehandle = urllib.urlopen(some_url, proxies={})
# Use proxies from environment - both versions are equivalent
filehandle = urllib.urlopen(some_url, proxies=None)
filehandle = urllib.urlopen(some_url)

例如我们用192.168.1.1:8080 为代理访问百度,代码如下

import urllib
proxies = {'http': 'http://192.168.1.1:8080'}
filehandle = urllib.urlopen("http://www.baidu.com", proxies=proxies)
print(filehandle.read())

相关文章

新浪微博 | 淘段子 | 我的博客 | 淘宝店铺 | jnan.org | RSS
Copyright © 2009 - 2011 酷符网