Python使用代理 之前用公司的网络执行代码一直报错,因为公司的网络设置了代理,无法直接访问,所以代码中需要设置代理。。。。pycharm报错信息:urllib2.URLError: 之前用公司的网络执行代码一直报错,因为公司的网络设置了代理,无法直接访问,所以代码中需要设置代理。。。。 pycharm报错信息:urllib2.URLError: <urlopen error [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond> #!/usr/bin/env python #-*- coding:utf-8 -*- import urllib2 url = "http://www.baidu.com/" #使用代理 proxy_support =urllib2.ProxyHandler({'http':'10.177.4.199:80'}) #参数是一个字典{‘类型’:‘代理ip:端口号’} opener = urllib2.build_opener(proxy_support) #定制opener opener.add_hadler = [('User-Agent','Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.101 Safari/537.36')] #add_handler 给加上伪装,伪装成浏览器访问 urllib2.install_opener(opener) response=urllib2.urlopen(url) print response.read()