我打开网址:
site = urllib2.urlopen('http://google.com')
我想要做的是用同样的方式连接我在某个地方告诉我:
site = urllib2.urlopen('http://google.com', proxies={'http':'127.0.0.1'})
但那也不起作用.
我知道urllib2有类似代理处理程序的东西,但我不记得那个功能.
操作系统:Windows 7; Python 2.7.3使用Python GUI Shell
我正在尝试通过Python阅读网站,有几位作者使用urllib
和urllib2
库.为了将网站存储在变量中,我看到了类似的方法:
import urllib
import urllib2
g = "http://www.google.com/"
read = urllib2.urlopen(g)
Run Code Online (Sandbox Code Playgroud)
最后一行在120秒后生成错误:
> Traceback (most recent call last): File "<pyshell#27>", line 1, in
> <module>
> r = urllib2.urlopen(o) File "C:\Python27\lib\urllib2.py", line 126, in urlopen
> return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 400, in open
> response = self._open(req, data) File "C:\Python27\lib\urllib2.py", line 418, in _open
> '_open', req) File "C:\Python27\lib\urllib2.py", line 378, in _call_chain
> result = func(*args) …
Run Code Online (Sandbox Code Playgroud) def check_web_server(host, port, path):
h = httplib.HTTPConnection(host, port)
h.request('GET',path)
resp = h.getresponse()
print 'HTTP Response:'
print ' status =', resp.status
print ' reason =', resp.reason
print 'HTTP Headers:'
for hdr in resp.getheaders():
print ' %s: %s' % hdr
Run Code Online (Sandbox Code Playgroud)
我这样调用了这个函数,check_web_server('www.python.org',80,'/')
但它给了我这个错误
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
你可以在这里清楚地看到代码 http://pastebin.com/V9KpGkvw
我在Stackoverflow搜索了这里,但我没有发现任何相关的问题抱歉我是网站的新手,如果我做错了.
我有一个perl程序从我的大学图书馆的数据库中检索数据,它运作良好.现在我想在python中重写它但遇到问题
<urlopen error [errno 104] connection reset by peer>
perl代码是:
my $ua = LWP::UserAgent->new;
$ua->cookie_jar( HTTP::Cookies->new() );
$ua->timeout(30);
$ua->env_proxy;
my $response = $ua->get($url);
Run Code Online (Sandbox Code Playgroud)
我写的python代码是:
cj = CookieJar();
request = urllib2.Request(url); # url: target web page
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj));
opener = urllib2.install_opener(opener);
data = urllib2.urlopen(request);
Run Code Online (Sandbox Code Playgroud)
我使用VPN(虚拟专用网络)在家里登录我的大学图书馆,我尝试了perl代码和python代码.perl代码按我的预期工作,但python代码总是遇到"urlopen错误".
我搜索了问题,似乎urllib2无法加载环境代理.但是根据urllib2的文档,urlopen()函数可以透明地使用不需要身份验证的代理.现在我感到很困惑.任何人都可以帮我解决这个问题吗?
我正在尝试下载页面的HTML(http://www.google.com
在这种情况下),但我收到了错误.这是我的交互式提示会话:
Python 3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import urllib
>>> import urllib.request
>>> html = urllib.request.urlopen("http://www.google.com")
Traceback (most recent call last):
File "\\****.****.org\myhome\python\lib\urllib\request.py", line 1136, in
do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "\\****.****.org\myhome\python\lib\http\client.py", line 964, in req
uest
self._send_request(method, url, body, headers)
File "\\****.****.org\myhome\python\lib\http\client.py", line 1002, in _s
end_request
self.endheaders(body)
File "\\****.****.org\myhome\python\lib\http\client.py", line 960, in end
headers
self._send_output(message_body)
File …
Run Code Online (Sandbox Code Playgroud)