gpt4 book ai didi

python - 为什么这个请求不起作用?

转载 作者:太空宇宙 更新时间:2023-11-04 01:42:06 25 4
gpt4 key购买 nike

我想使用 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")

我得到这个错误:

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>

为什么??

最佳答案

代码看起来没问题。

以下工作。

>>> 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

另一个是twitter居然没 react 过来。这种情况在 Twitter 上经常发生。

关于python - 为什么这个请求不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3964519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com