当前位置:编程学习 > 网站相关 >>

python urlOpen使用代理

利用urllib包
Java代码 
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包
Java代码 
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
Java代码 
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);

补充:Web开发 , Python ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,