Can someone please tell me how to get one username from one id on tweepy? I have looked everywhere and can't find anything.
If you just have a user_id value you need to call the twitter API with the get_user(user_id) method.
The returned User object will contain the username under screen_name.
# steps not shown where you set up api
u = api.get_user(783214)
print u.screen_name
If you already have the User object from another API call just look for the screen_name.
You can use this code to get user screen name or user id
To get user screen name from user id
In [36]: import tweepy
In [37]: consumer_key = Your_consumerkey
In [38]: consumer_secret = Your_consuersecret
In [39]: access_token = Your_access_token
In [40]: access_token_secret = Your_access_token_secret
In [41]: auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
In [42]: auth.set_access_token(access_token, access_token_secret)
In [43]: api = tweepy.API(auth)
In [48]: user = api.get_user(1088398616)
In [49]: user.screen_name
Out[49]: u'saimadhup'
To get user id from user screen name
In [46]: user = api.get_user(screen_name = 'saimadhup')
In [47]: user.id
Out[47]: 1088398616
Although OP clearly needs the username for just one id, in case if one wants to get usernames for a list of ids(<100), then:
def get_usernames(ids):
""" can only do lookup in steps of 100;
so 'ids' should be a list of 100 ids
"""
user_objs = api.lookup_users(user_ids=ids)
for user in user_objs:
print(user.screen_name)
For larger set of ids, you can just put this in a for loop and call accordingly while obeying the twitter API limit.
Related
I'm new to Python, and so I'm struggling a bit with this. Basically, the code below gets the text of tweets with the hashtag bitcoin in it, and I want to extract the date and author as well as the text. I've tried different things, but stuck rn.
Greatly appreciate any help with this.
import pandas as pd
import numpy as np
import tweepy
api_key = '*'
api_secret_key = '*'
access_token = '*'
access_token_secret = '*'
authentication = tweepy.OAuthHandler(consumer_key, consumer_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(authentication, wait_on_rate_limit=True)
#Get tweets about Bitcoin and filter out any retweets
search_term = '#bitcoin -filter:retweets'
tweets = tweepy.Cursor(api.search_tweets, q=search_term, lang='en', since='2018-11-01', tweet_mode='extended').items(50)
all_tweets = [tweet.full_text for tweet in tweets]
df = pd.DataFrame(all_tweets, columns=['Tweets'])
df.head()
If you use dir(tweet) then you see all variables and functions in object tweet
author
contributors
coordinates
created_at
destroy
display_text_range
entities
extended_entities
favorite
favorite_count
favorited
full_text
geo
id
id_str
in_reply_to_screen_name
in_reply_to_status_id
in_reply_to_status_id_str
in_reply_to_user_id
in_reply_to_user_id_str
is_quote_status
lang
metadata
parse
parse_list
place
possibly_sensitive
retweet
retweet_count
retweeted
retweets
source
source_url
truncated
user
And there is created_at
all_tweets = []
for tweet in tweets:
#print('\n'.join(dir(tweet)))
all_tweets.append( [tweet.full_text, tweet.created_at] )
df = pd.DataFrame(all_tweets, columns=['Tweets', 'Created At'])
df.head()
Result:
Tweets Created At
0 #Ralvero Of course $KAWA ready for 100x 🚀#ETH ... 2022-03-26 13:51:06+00:00
1 Pairs:1INCHUSDT \n SELL:1.58500\n Time :3/26/2... 2022-03-26 13:51:06+00:00
2 #hotcrosscom #iSafePal 🌐 First LIVE Dapp: Cylu... 2022-03-26 13:51:04+00:00
3 #Justdoitalex #Isabel_Schnabel Finally a truth... 2022-03-26 13:51:03+00:00
4 #Bitcoin has rejected for the fourth time the ... 2022-03-26 13:50:55+00:00
But your code have problem with since because it seems it was removed in version 3.8
See: Collect tweets in a specific time period in Tweepy, until and since doesn't work
i had build code to sentiment analysis using python, my code was like this :
import tweepy
api_key = "sdlksadksa;ldksald"
api_secret_key = "sakdlas,mcsdmv,dlv"
access_token = "alskdklamlas"
access_token_secret = "salkdjklmclqm"
auth = tweepy.OAuthHandler(api_key, api_secret_key)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
hasilUser = api.user_timeline(id="jokowi" , count = 10)
in case to you guys know, my api_key until access_token_secret are dummy and not the real one.
when i run hasilUser idk why it turn like this
Unexpected parameter: id
i had no idea what is going on and what should i do
I think input parameters of API.user_timeline changed(may be from id to user_id).
You can see the actual parameters of it in the code below, 530th line.
https://github.com/tweepy/tweepy/blob/master/tweepy/api.py
#pagination(mode='id')
#payload('status', list=True)
def user_timeline(self, **kwargs):
"""user_timeline(*, user_id, screen_name, since_id, count, max_id, \
trim_user, exclude_replies, include_rts)
Returns the 20 most recent statuses posted from the authenticating user
or the user specified. It's also possible to request another user's
timeline via the id parameter.
Parameters
----------
user_id
|user_id|
screen_name
|screen_name|
since_id
|since_id|
count
|count|
max_id
|max_id|
trim_user
|trim_user|
exclude_replies
|exclude_replies|
include_rts
When set to ``false``, the timeline will strip any native retweets
(though they will still count toward both the maximal length of the
timeline and the slice selected by the count parameter). Note: If
you're using the trim_user parameter in conjunction with
include_rts, the retweets will still contain a full user object.
Returns
-------
:py:class:`List`\[:class:`~tweepy.models.Status`]
References
----------
https://developer.twitter.com/en/docs/twitter-api/v1/tweets/timelines/api-reference/get-statuses-user_timeline
"""
return self.request(
'GET', 'statuses/user_timeline', endpoint_parameters=(
'user_id', 'screen_name', 'since_id', 'count', 'max_id',
'trim_user', 'exclude_replies', 'include_rts'
), **kwargs
)
I need to consolidate these two tweet datasets into a single variable. The variable needs to have two "columns," one for the text of the tweets, the other a binary indicator of the source (e.g. 0 for the first source, 1 for the second). I can use a list of tuples or a Pandas dataframe. I am brand new to coding, so I am not sure how to proceed. I understand that I could create two dictionaries and combine them, but not sure how to add the column that contains the binary indicator. This is where I am now:
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)
userNRA = api.get_user("NRA")
userCSGV = api.get_user("CSGV")
c_nra = tweepy.Cursor(api.user_timeline, id="NRA")
NRAtweet_store = []
for status in c_nra.items(500):
NRAtweet_store.append(status.text)
c_csgv = tweepy.Cursor(api.user_timeline, id="CSGV")
CSGVtweet_store = []
for status in c_csgv.items(500):
CSGVtweet_store.append(status.text)
Rather than appending just the text, append the text and a flag:
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)
userNRA = api.get_user("NRA")
userCSGV = api.get_user("CSGV")
tweets = []
c_nra = tweepy.Cursor(api.user_timeline, id="NRA")
for status in c_nra.items(500):
tweets.append((status.text, 0))
c_csgv = tweepy.Cursor(api.user_timeline, id="CSGV")
for status in c_csgv.items(500):
tweets.append((status.text, 1))
This will leave you with one list of tuples, with the second entry in each tuple indicating the source of the first entry.
I created a scraper with python that gets all the followers of a particular twitter user. The issue is that when I use this list of user Ids to get their tweets with logstash, I have an Error.
I used http://gettwitterid.com/ to manually check if these Ids are working, and they are but the list is really long to check it one by one.
Is there a solution with python to split the Ids into two lists, one containing Valid Ids and the other contains the Not valid ones, thet I use the Valid list as input for logstash?
The first 10 rows of the csv file is like this :
"id"
"602169027"
"95104995"
"874339739557670912"
"2981270769"
"93054327"
"870723159011545088"
"3008493180"
"874804469082533888"
"756339889092829184"
"1077712806"
I tried this code to get tweets using Ids imported from csv, but unfortunetly it's raising 144 (Not found)
import tweepy
import pandas as pd
consumer_key = ""
consumer_secret = ""
access_token_key = "-"
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token_key, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
dfuids = pandas.read_csv('Uids.csv')
for index, row in dfuids.iterrows():
print row['id']
tweet = api.get_status(dfuids['id'])
importing ids from csv
Try to change your code to this:
for index, row in dfuids.iterrows():
print row['id']
tweet = api.get_status(row['id'])
To escape potential errors, you can add a try / except loop later.
I got the solution after some experiments:
dfuids = pd.read_csv('Uids.csv')
valid = []
notvalid = []
for index, row in dfuids.iterrows():
print index
x = str(row.id)
#print x , type(x)
try:
tweet = api.user_timeline(row.id)
#print "Fine :",row.id
valid.append(x)
#print x, "added to valid"
except:
#print "NotOk :",row.id
notvalid.append(x)
#print x, "added to valid"
This Part of the code was what I needed, so it loops for all the Ids, and test if that user id give us some tweets from the timeline, if correct then it's appended as string to a list called (valid) else if we have an exception for any reason then it's appended to (notvalid).
We can save this list into a dataframe and export csv :
df = pd.DataFrame(valid)
dfnotv = pd.DataFrame(notvalid)
df.to_csv('valid.csv', index=False, encoding='utf-8')
dfnotv.to_csv('notvalid.csv', index=False, encoding='utf-8')
I am trying to go through a list of tweets related to a specific search term and trying to extract all the hashtags. I wish to make a python list which includes all the hashtags. I started by using Twython as follows
from twython import Twython
api_key = 'xxxx'
api_secret = 'xxxx'
acces_token = 'xxxx'
ak_secret = 'xxxx'
t = Twython(app_key = api_key, app_secret = api_secret, oauth_token = acces_token, oauth_token_secret = ak_secret)
search = t.search(q = 'Python', count = 10)
tweets = search['statuses']
hashtags = []
for tweet in tweets:
b = (tweet['text'],"\n")
if b.startswith('#'):
hastags.append(b)
It doesn't seem to be working. I get the error that
'tuple object has no attribute startswith'
I am not sure if I am meant to make a list of all the statuses first and extract using the mentioned method. Or it is okay to proceed without making the list of statuses first.
Thank you
That is correct, strings have the startswith attribute and tuples do not.
Change the last three lines to this:
b = (tweet['text'])
if b.startswith("#") is True:
hashtags.append(b)
If you really want that line break then it would be:
b = (tweet['text'] + "\n")
if b.startswith("#") is True:
hashtags.append(b)