为什么这个请求不起作用? [英] Why this request doesn't work?

查看:41
本文介绍了为什么这个请求不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 Twitter API 制作一个简单的愚蠢的 Twitter 应用程序.

如果我从浏览器请求此页面,它确实可以工作:

http://search.twitter.com/search.atom?q=hello&rpp=10&page=1

但是如果我使用 urllib 或 urllib2 从 python 请求这个页面,大多数时候它不起作用:

response = urllib2.urlopen("http://search.twitter.com/search.atom?q=hello&rpp=10&page=1")

我收到此错误:

回溯(最近一次调用最后一次): 中的文件twitter.py",第 24 行response = urllib2.urlopen("http://search.twitter.com/search.atom?q=hello&rpp=10&page=1")文件/usr/lib/python2.6/urllib2.py",第 126 行,在 urlopenreturn _opener.open(url, data, timeout)文件/usr/lib/python2.6/urllib2.py",第391行,打开response = self._open(req, data)文件/usr/lib/python2.6/urllib2.py",第 409 行,在 _open'_open',请求)_call_chain 中的文件/usr/lib/python2.6/urllib2.py",第 369 行结果 = func(*args)文件/usr/lib/python2.6/urllib2.py",第 1161 行,在 http_open返回 self.do_open(httplib.HTTPConnection, req)文件/usr/lib/python2.6/urllib2.py",第 1136 行,在 do_open引发 URLError(err)urllib2.URLError: <urlopen error [Errno 110] 连接超时>

为什么??

解决方案

代码看起来没问题.

以下有效.

<预><代码>>>>导入 urllib>>>导入 urllib2>>>user_agent = 'curl/7.21.1 (x86_64-apple-darwin10.4.0) libcurl/7.21.1'>>>url='http://search.twitter.com/search.atom?q=hello&rpp=10&page=1'>>>标头 = { '用户代理':user_agent }>>>req = urllib2.Request(url, None, headers)>>>响应 = urllib2.urlopen(req)>>>the_page = response.read()>>>打印_page

另一个是twitter居然无法响应.这种情况在 Twitter 上经常发生.

I want to make a simple stupid twitter app using Twitter API.

If I request this page from my browser it does work:

http://search.twitter.com/search.atom?q=hello&rpp=10&page=1

but if I request this page from python using urllib or urllib2 most of the times it doesn't work:

response = urllib2.urlopen("http://search.twitter.com/search.atom?q=hello&rpp=10&page=1")

and I get this error:

Traceback (most recent call last):
  File "twitter.py", line 24, in <module>
    response = urllib2.urlopen("http://search.twitter.com/search.atom?q=hello&rpp=10&page=1")
  File "/usr/lib/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.6/urllib2.py", line 391, in open
    response = self._open(req, data)
  File "/usr/lib/python2.6/urllib2.py", line 409, in _open
    '_open', req)
  File "/usr/lib/python2.6/urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.6/urllib2.py", line 1161, in http_open
    return self.do_open(httplib.HTTPConnection, req)
  File "/usr/lib/python2.6/urllib2.py", line 1136, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno 110] Connection timed out>

Why ??

解决方案

The code seems alright.

The following worked.

>>> import urllib
>>> import urllib2
>>> user_agent = 'curl/7.21.1 (x86_64-apple-darwin10.4.0) libcurl/7.21.1'
>>> url='http://search.twitter.com/search.atom?q=hello&rpp=10&page=1'
>>> headers = { 'User-Agent' : user_agent }
>>> req = urllib2.Request(url, None, headers)
>>> response = urllib2.urlopen(req)
>>> the_page = response.read()
>>> print the_page

The other is twitter actually could not respond. This happens once too often with Twitter.

这篇关于为什么这个请求不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆