使用 urllib.request returns 代理自动配置文件
Using urllib.request returns Proxy Auto-Config file
我正在使用来自 here 的 Martin Konecny 代码从我的公司防火墙后面查询一个 http 站点:
代码是这样的:
import urllib.request
req = urllib.request.Request(
'http://www.espncricinfo.com/',
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
f = urllib.request.urlopen(req)
g = open('writing.txt','w')
g.write(f.read().decode('utf-8'))
g.close
但是,一旦我 运行 此代码,我收到 PAC 文件而不是 url 的内容。
如何通过它来下载给定 url 的网站内容?
谢谢!
import urllib.request
req = urllib.request.Request('http://www.espncricinfo.com/', data=None, headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
proxy_support = urllib.request.ProxyHandler({'http': 'ip:port'})
opener = urllib.request.build_opener(proxy_support)
# make opener object the global default opener.
urllib.request.install_opener(opener)
f = urllib.request.urlopen(req)
g = open('writing.txt','w')
g.write(f.read().decode('utf-8'))
g.close
我正在使用来自 here 的 Martin Konecny 代码从我的公司防火墙后面查询一个 http 站点:
代码是这样的:
import urllib.request
req = urllib.request.Request(
'http://www.espncricinfo.com/',
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
f = urllib.request.urlopen(req)
g = open('writing.txt','w')
g.write(f.read().decode('utf-8'))
g.close
但是,一旦我 运行 此代码,我收到 PAC 文件而不是 url 的内容。
如何通过它来下载给定 url 的网站内容?
谢谢!
import urllib.request
req = urllib.request.Request('http://www.espncricinfo.com/', data=None, headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
proxy_support = urllib.request.ProxyHandler({'http': 'ip:port'})
opener = urllib.request.build_opener(proxy_support)
# make opener object the global default opener.
urllib.request.install_opener(opener)
f = urllib.request.urlopen(req)
g = open('writing.txt','w')
g.write(f.read().decode('utf-8'))
g.close