简体   繁体   中英

Cant figure out Tweepy Twitter Search API code

I am very new to Python having taught it to myself just a few weeks ago. I have tried to cobble together some simple script using Tweepy to do various things with the Twitter API. I have been trying to get the Search API working but to no avail. I have the following code just to simply Search the last 7 days of Twitter for keywords.

# 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

Every time I get the following error:

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

I also tried

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

But then I get:

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__'

Can anyone please point me in the right direction, this has been driving me nuts for the past few days.

Thanks.

First of all, you are importing some things wrong, you might want to read more about how import works: What are good rules of thumb for Python imports?

A working example of how to make this kind of search work:

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

Also, I would recommend to avoid spaces in filenames, instead of "tweepy streaming.py" go for something like "tweepy_streaming.py".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM