请求twitter api时urllib2的问题

时间:2013-10-25 12:01:27

标签: python twitter urllib2

我正在使用twitter api包装器进行一些调用,但我得到了这个回溯:

Traceback (most recent call last):
  File "tweets.py", line 100, in <module>
    print stream_country('marketing','fr') + stream_worldwide('marketing') + stream_country('marketing','it') +  stream_country('marketing','en')
  File "tweets.py", line 41, in stream_country
    search = t.search.tweets(q = keyword, lang = 'fr', count = 100, since_id = max_ID)
  File "/usr/local/lib/python2.7/dist-packages/twitter/api.py", line 204, in __call__
    return self._handle_response(req, uri, arg_data, _timeout)
  File "/usr/local/lib/python2.7/dist-packages/twitter/api.py", line 211, in _handle_response
    handle = urllib_request.urlopen(req, **kwargs)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 400, in open
    response = self._open(req, data)
  File "/usr/lib/python2.7/urllib2.py", line 418, in _open
    '_open', req)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 1215, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib/python2.7/urllib2.py", line 1177, in do_open
    raise URLError(err)
urllib2.URLError: <urlopen error [Errno -3] Temporary failure in name resolution>

代码:

from twitter import *
t = Twitter(
    auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET,
               CONSUMER_KEY, CONSUMER_SECRET)
)
since_ID = -1
max_ID = 0
tcount = 0
while(since_ID != max_ID):
    search = t.search.tweets(q = keyword, lang = 'fr', count = 100, since_id = max_ID)

    #print len(search['statuses'])
    if len(search['statuses']) == 0:
        #print 'end'
        break

    since_ID = search['search_metadata']['since_id_str']
    max_ID = search['search_metadata']['max_id_str']

    for doc in search['statuses']:
        #print doc['text']
        tcount += 1

return tcount 

0 个答案:

没有答案