簡體   English   中英

當我嘗試根據特定用戶的特定術語檢索推文時,python拋出錯誤

[英]python throwing an error when I am trying to retrieve tweets based on specific terms from specific users

from __future__ import unicode_literals
import tweepy
consumer_key='xyz',
consumer_secret='xyz',
access_key='xyz',
access_secret='xyz'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api=tweepy.API(auth)
class CustomStreamListener(tweepy.StreamListener):
    def on_status(self, status):
        return True
sapi = tweepy.streaming.Stream(auth, CustomStreamListener())
list_users = ['17006157','59145948','157009365','16686144','68044757','33338729']   #Some ids
list_terms = ['abc','def']   #Some terms
sapi.filter(follow=list_users, track=list_terms)

python shell上的輸出是

Traceback (most recent call last):
  File "C:/Python27/nytimes/20052014/mutliple keyword from multiple user search.py", line 44, in <module>
    sapi.filter(track=["list_terms"])
  File "build\bdist.win32\egg\tweepy\streaming.py", line 313, in filter
    self._start(async)
  File "build\bdist.win32\egg\tweepy\streaming.py", line 235, in _start
    self._run()
  File "build\bdist.win32\egg\tweepy\streaming.py", line 151, in _run
    timeout=self.timeout, stream=True, auth=auth)
  File "C:\Python27\lib\site-packages\requests-2.1.0-py2.7.egg\requests\sessions.py", line 348, in request
    prep = self.prepare_request(req)
  File "C:\Python27\lib\site-packages\requests-2.1.0-py2.7.egg\requests\sessions.py", line 286, in prepare_request
    hooks=merge_hooks(request.hooks, self.hooks),
  File "C:\Python27\lib\site-packages\requests-2.1.0-py2.7.egg\requests\models.py", line 290, in prepare
    self.prepare_auth(auth, url)
  File "C:\Python27\lib\site-packages\requests-2.1.0-py2.7.egg\requests\models.py", line 469, in prepare_auth
    r = auth(self)
  File "C:\Python27\lib\site-packages\requests_oauthlib-0.4.0-py2.7.egg\requests_oauthlib\oauth1_auth.py", line 63, in __call__
    unicode(r.url), unicode(r.method), r.body or '', r.headers)
  File "build\bdist.win32\egg\oauthlib\oauth1\rfc5849\__init__.py", line 280, in sign
    request.oauth_params.append(('oauth_signature', self.get_oauth_signature(request)))
  File "build\bdist.win32\egg\oauthlib\oauth1\rfc5849\__init__.py", line 112, in get_oauth_signature
    uri, headers, body = self._render(request)
  File "build\bdist.win32\egg\oauthlib\oauth1\rfc5849\__init__.py", line 186, in _render
    headers = parameters.prepare_headers(request.oauth_params, request.headers, realm=realm)
  File "build\bdist.win32\egg\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
    return target(params, *args, **kwargs)
  File "build\bdist.win32\egg\oauthlib\oauth1\rfc5849\parameters.py", line 58, in prepare_headers
    escaped_value = utils.escape(value)
  File "build\bdist.win32\egg\oauthlib\oauth1\rfc5849\utils.py", line 57, in escape
    'Got %s of type %s.' % (u, type(u)))
ValueError: Only unicode objects are escapable. Got <generator object <genexpr> at 0x03006080> of type <type 'generator'>.

僅此而已。

看起來這是三個變量賦值結尾處的逗號:

consumer_key='xyz',
consumer_secret='xyz',
access_key='xyz',

逗號將值轉換為元組。 由於某種原因,oauth庫將它們轉換為生成器,從而導致ValueError升高。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM