Hi I was wondering if anyone could help:
I have the code beneath that contacts Twitter's API in order to get retweet count from a Twitter ID no. I was wondering if anyone could point me in the right direction as to how to enter the code so that it skips ID numbers the module (Tweepy) doesnt recognise instead of just stopping altogether?
EDIT: The Error being "TweepError: [{u'message': u'Sorry, that page does not exist', u'code': 34}]"
Thanks!
import tweepy
consumer_key=""
consumer_secret=""
access_key = ""
access_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
id_list = ["4000000000000",
"etc.....",
]
def get_retweet_count(tweet_id):
tweet = api.get_status(tweet_id)
return tweet.retweet_count
for id in id_list:
print get_retweet_count(id), id
You want to use exception handling -- i.e. put a try except clause around any code that throws exceptions you think you can handle.
eg.
try:
print get_retweet_count(id)
except TweepError, e:
print e
print "carrying on anyway"
As thecoder mentioned, a try - except block should solve the problem:
for id in id_list:
try:
print get_retweet_count(id), id
except:
pass
Related
I am trying to scrape Twitter profiles for a project I am doing. I have the following code
from tweepy import OAuthHandler
import pandas as pd
"""I like to have my python script print a message at the beginning. This helps me confirm whether everything is set up correctly. And it's nice to get an uplifting message ;)."""
print("You got this!")
access_token = ''
access_token_secret = ''
consumer_key = ''
consumer_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
tweets = []
count = 1
"""Twitter will automatically sample the last 7 days of data. Depending on how many total tweets there are with the specific hashtag, keyword, handle, or key phrase that you are looking for, you can set the date back further by adding since= as one of the parameters. You can also manually add in the number of tweets you want to get back in the items() section."""
for tweet in tweepy.Cursor(api.search, q="#BNonnecke", count=450, since='2020-02-28').items(50000):
print(count)
count += 1
try:
data = [tweet.created_at, tweet.id, tweet.text, tweet.user._json['screen_name'], tweet.user._json['name'], tweet.user._json['created_at'], tweet.entities['urls']]
data = tuple(data)
tweets.append(data)
except tweepy.TweepError as e:
print(e.reason)
continue
except StopIteration:
break
df = pd.DataFrame(tweets, columns = ['created_at','tweet_id', 'tweet_text', 'screen_name', 'name', 'account_creation_date', 'urls'])
"""Add the path to the folder you want to save the CSV file in as well as what you want the CSV file to be named inside the single quotations"""
df.to_csv(path_or_buf = '/Users/Name/Desktop/FolderName/FileName.csv', index=False)
however, I keep getting the error "API" object has no attribute "search" from the line "for tweet in tweepy.Cursor(api.search, q="#BNonnecke", count=450, since='2020-02-28').items(50000):" I am not really sure why and don't know how to resolve this issue.
Thanks so much!
The latest version of Tweepy (v4 upwards) now has a search_tweets method instead of a search method. Check the documentation.
API.search_tweets(q, *, geocode, lang, locale, result_type, count, until, since_id, max_id, include_entities)
Also, read the comment in your code :-) The Search API has a 7 day history limit, so searching for Tweets since 2020-02-28 will only return Tweets posted in the 7 days before the date you run your code.
I am a beginner at python . I'm trying to get the follower counts of a given user handle from twitter. The issue is that tweepy is not connecting to twitter and is not even returning any error. The terminal just stays blank. Please help on this.
import tweepy
import pymysql
import time
#insert your Twitter keys here
consumer_key =''
consumer_secret=''
access_token=''
access_secret=''
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
global conn
conn=pymysql.connect(db='twitter', user='root' , host= 'localhost' , port=3307)
global cursor
cursor=conn.cursor()
print("entering loop")
while True:
query=cursor.execute("select twitter_name from timj_users where found_followers is null and twitter_name is not null order by id asc limit 1")
if query>0:
results=cursor.fetchone()
timj_handle=results[0]
user = tweepy.Cursor(api.followers, screen_name=timj_handle).items()
try:
followers=user.follower_count
location=user.location
cursor.execute("update timj_users set followers=%s,location=%s,found_followers=1 where twitter_name=%s" , (followers, location ,handle))
conn.commit()
print("user followers received")
if followers>100:
user.follow()
cursor.execute("update users set followed=1 where twitter_name=%s" , (handle))
conn.commit()
print("User followed")
except:
time.sleep(15*60)
print 'We got a timeout ... Sleeping for 15 minutes'
else:
print("All users processed")
break
If you're not getting an error from python and the console is just "hanging" you did actually connect to the Twitter, but since you have nothing in the code to display any message you get from Twitter you won't receive anything.
You need to include this in the code:
def on_error(self, status_code):
print(status_code)
That code will give you provide you with the number that is related to Twitter's Error Codes & Responses.
To be more clear:
except:
time.sleep(15*60)
print 'We got a timeout ... Sleeping for 15 minutes'
That exception is not being used what you think it is. The exception is raised if there's an error in the code you're writing, not errors you obtain from twitter.
It looks like this line
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
should be
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
However, you have written a really complicated for a beginner. Can I suggest that you run a more basic Tweepy program, and see what you get.
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
public_tweets = api.home_timeline()
for tweet in public_tweets:
print tweet.text
(From the Tweepy Documentation)
I'm collecting tweets for a big number of users, so the script will run for days/weeks unsupervised.
I have a list of user_ids in big_list.
I think some of the tweets are private and my script stops so I'd like a way for the script to continue on to the next user_id (and maybe print a warning message).
I'd also like suggestions on how to make it robust to other errors or exceptions (for example, for the script to sleep on error or timeout)
This is a summary of what I have:
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
my_api = tweepy.API(auth)
for id_str in big_list:
all_tweets = get_all_tweets(id_str=id_str, api=my_api)
#Here: insert some tweets into my database
The get_all_tweets function throws the errors and it basically repeatedly calls:
my_api.user_timeline(user_id = id_str, count=200)
Just in case, the traceback it gives is the following:
/home/username/anaconda/lib/python2.7/site-packages/tweepy/binder.pyc in execute(self)
201 except Exception:
202 error_msg = "Twitter error response: status code = %s" % resp.status
--> 203 raise TweepError(error_msg, resp)
204
205 # Parse the response payload
TweepError: Not authorized.
Let me know if you need more details. Thanks!
----------- EDIT --------
This question has some info.
I guess I can try to do a try/except block for different type of errors? I don't know of all the relevant, so best practices of someone with field experience would be appreciated!
---------- EDIT 2 -------
I'm getting some Rate limit exceeded errors so I'm making the loop sleep like this. The else part would handle the "Not authorized" error and some other (unknown?) errors. This still makes me loose an element in the big_list though.
for id_str in big_list:
try:
all_tweets = get_all_tweets(id_str=id_str, api=my_api)
# HERE: save tweets
except tweepy.TweepError, e:
if e == "[{u'message': u'Rate limit exceeded', u'code': 88}]":
time.sleep(60*5) #Sleep for 5 minutes
else:
print e
You may just do a "pass" :
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
my_api = tweepy.API(auth)
for id_str in big_list:
try:
all_tweets = get_all_tweets(id_str=id_str, api=my_api)
except Exception, e:
pass
I am really late on this but I faced the same problem during these days. For the need of a time.sleep() I solved the problem thanks to alecxe reply to this question.
I am diving in the past but I hope this will help someone in the future.
You can just use "wait_on_ratelimit" and "wait_on_rate_limit_notify" of Tweepy when you create the API object and add a general tweepy error handling, then, with the specific erros shown, you may try to personalize the code handling each error. It should be something like this:
import tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
my_api = tweepy.API(auth, wait_on_rate_limit = True, wait_on_rate_limit_notify = True)
for id_str in big_list:
try:
all_tweets = get_all_tweets(id_str=id_str, api=my_api)
except tweepy.TweepError as e:
print("Tweepy Error: {}".format(e))
I want to search the tweets, extract the screen_name from the statuses and use that to get the past favorite tweets of that screen_name. Now I donot know how to extract the screen_names from statuses and then how to use to get past favorites. This is my code(the problem is with the last line):
import tweepy
consumer_key = ''
consumer_secret = ''
access_token = ''
access_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
search_results = tweepy.Cursor(api.search,q="Ganesh World").items()
for i in search_results:
print i.text.encode('utf-8')
print api.favorites([i.screen_name])
It is throwing AttributeError:Status object has no attribute 'screen_name'
No need to waste another API call,
You can fetch it from inner data elements, i.e. from author , and within that from its json like this:
for i in search_results:
print i.author._json['screen_name']
Try this:
for i in search_results:
print i.text.encode('utf-8')
user_details = api.get_user(user_id = i.from_user_id)
print api.favorites([user_details.screen_name])
You are trying to get screen_name directly from the tweet. First you should get id and then use it to get screen name.
you have to do:
i.user.screen_name
I was working with this code. Until I included print tweepy in the code the page on GAE returned as blank; no error, or anything. It worked fine from the command line. Why would this happen? Is there a better way to make this work?
import tweepy
print tweepy
CONSUMER_KEY = "mine"
CONSUMER_SECRET = "mine"
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
try:
print auth.get_authorization_url()
except Exception,e: print str(e)