Skip to main content
 首页 » 编程设计

当我尝试根据特定用户的特定术语检索推文时,python 抛出错误

2025年05月04日10飞鱼

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 被引发。