【问题标题】:Cant figure out Tweepy Twitter Search API code无法弄清楚 Tweepy Twitter 搜索 API 代码
【发布时间】:2014-09-03 20:53:46
【问题描述】:

我对 Python 还是很陌生,几周前我还自学了它。我尝试使用 Tweepy 拼凑一些简单的脚本来使用 Twitter API 做各种事情。我一直在尝试让 Search API 工作,但无济于事。我有以下代码只是为了简单地在 Twitter 的最后 7 天搜索关键字。

# 1.Import required libs and used objects/libs 
import tweepy
from tweepy import OAuthHandler
from tweepy import API
from tweepy import Cursor



#2. GET or input App keys and tokens. Here keys/tokens are pasted from Twitter.
ckey = 'key'
csecret = 'secret'
atoken = 'token'
asecret =  'secret'

# 3. Set authorization tokens. 
auth = tweepy.OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

#4. Define API. 
api = tweepy.API(auth)

#5. Define list or library.

for tweets in tweepy.Cursor(api.search, q = '#IS', count = 100,
                           result_type ='recent').items():
print tweet.text

每次我收到以下错误:

Traceback (most recent call last):
  File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in <module>
     result_type ='recent')).items():
   File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 22, in __init__
    raise TweepError('This method does not perform pagination')
TweepError: This method does not perform pagination

我也试过了

for tweets in tweepy.Cursor(api.search(q = '#IS', count = 100,
                           result_type ='recent')).items():
print tweet.text

然后我得到:

Traceback (most recent call last):
  File "C:/Users/jbutk_001/Desktop/Tweety Test/tweepy streaming.py", line 25, in   <module>
    result_type ='recent').items():
  File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 181, in next
     self.current_page = self.page_iterator.next()
   File "build\bdist.win-amd64\egg\tweepy\cursor.py", line 101, in next
    old_parser = self.method.__self__.parser
 AttributeError: 'function' object has no attribute '__self__'

谁能指出我正确的方向,过去几天这一直让我发疯。

谢谢。

【问题讨论】:

    标签: python api search twitter tweepy


    【解决方案1】:

    首先,您导入了一些错误的内容,您可能想了解更多有关导入如何工作的信息: What are good rules of thumb for Python imports?

    如何使这种搜索工作的工作示例:

    import tweepy
    
    CONSUMER_KEY = 'key'
    CONSUMER_SECRET = 'secret'
    ACCESS_KEY = 'accesskey'
    ACCESS_SECRET = 'accesssecret'
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
    api = tweepy.API(auth)
    
    for tweet in tweepy.Cursor(api.search,
                           q="#IS",
                           count=100,
                           result_type="recent",
                           include_entities=True,
                           lang="en").items():
    print tweet.tweet
    

    另外,我建议避免在文件名中使用空格,而不要使用“tweepy streaming.py”,而是使用“tweepy_streaming.py”之类的内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-10
      • 2018-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多