无法找出Tweepy Twitter搜索API代码

时间:2014-07-14 00:15:44

标签: python api search twitter tweepy

几周前,我对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__'

任何人都可以指出我正确的方向,过去几天这让我疯了。

感谢。

1 个答案:

答案 0 :(得分:3)

首先,您导入了一些错误的内容,您可能想要了解有关导入如何工作的更多信息: 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

另外,我建议避免文件名中的空格,而不是&#34; tweepy streaming.py&#34;去找像&#34; tweepy_streaming.py&#34;。