how to collect twitter data from multiple IDs - python

I have a list of 14 user IDs and I have to collect them from each of the users.I run this Code to get the IDs from different Account. How could I get data from all the accounts?
# assign the values accordingly
consumer_key = 'XXXX'
consumer_key_secret = 'XXXXX'
access_token = 'XXXXX'
access_token_secret = 'XXXXXX'
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the screen name of the user
screen_name = ["m","b","c"]
ID = []
# fetching the user
for i in range(len(screen_name)):
user = api.get_user(screen_name[i])
#fetching the ID
ID.append(user.id_str)
del(user)
ID
i got this
['15454564','25645464','35456464']
HOW CAN I GET THE DATA FROM MULTIPLE IDS

To get data from multiple Tweet IDs, just create separate variables or arrays to store the data you want. And to pull data from Tweet IDs, just check the Tweepy documentation at https://docs.tweepy.org/en/v3.5.0/. Good luck, and let me know if you have any other questions.

Related

get all retweets user-id for each tweet-id

I have a list of tweets Id more than 100 and I want to get all retweets Id for each tweet Id the code that I used is for one tweet Id how can I give the list of tweets Id and check if there is retweets for this tweet print the user ids
# import the module
import tweepy
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the ID of the tweet
ID = 1265889240300257280
# getting the retweeters
retweets_list = api.retweets(ID)
# printing the screen names of the retweeters
for retweet in retweets_list:
print(retweet.user.screen_name)
can anyone help me ?
For getting Retweets from a list of Tweets, you'll need to iterate over your list of Tweet IDs and call the api.retweets function for each one in turn.
If your Tweets themselves have more than 100 Retweets, you'll hit a limitation in the API.
Per the Tweepy documentation:
API.retweets(id[, count])
Returns up to 100 of the first retweets of the given tweet.
The Twitter API itself only supports retrieving up to 100 Retweets, see the API documentation (this is the same API that Tweepy is calling):
GET statuses/retweets/:id
Returns a collection of the 100 most recent retweets of the Tweet specified by the id parameter.
This works for me:
for retweet in retweets_list:
print (retweets_list.retweet_count)

Error scraping friend list from Twitter user w/ api.lookup_users: Too many terms specified in query

The code below was provided to another user who was scraping the "friends" (not followers) list of a specific Twitter user. For some reason, I get an error when using "api.lookup_users". The error states "Too many terms specified in query". Ideally, I would like to scrape the followers and output a csv with the screen names (not ids). I would like their descriptions as well, but can do this in a separate step unless there is a suggestion for pulling both pieces of information. Below is the code that I am using:
import time
import tweepy
import csv
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.friends, screen_name="").pages():
ids.extend(page)
time.sleep(60)
print(len(ids))
users = api.lookup_users(user_ids=ids) #iterates through the list of users and prints them
for u in users:
print(u.screen_name)
From the error you get, it seems that you are putting too many ids at once in the api.lookup_users request. Try splitting you list of ids in smaller parts and make a request for each part.
import time
import tweepy
import csv
#Twitter API credentials
consumer_key = ""
consumer_secret = ""
access_key = ""
access_secret = ""
auth = tweepy.auth.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.friends, screen_name="").pages():
ids.extend(page)
time.sleep(60)
print(len(ids))
idChunks = [ids[i:i + 300] for i in range(0, len(ids), 300)]
users = []
for idChunk in idChunks:
try:
users.append(api.lookup_users(user_ids=idChunk))
except tweepy.error.RateLimitError:
print("RATE EXCEDED. SLEEPING FOR 16 MINUTES")
time.sleep(16*60)
users.append(api.lookup_users(user_ids=idChunk))
for u in users:
print(u.screen_name)
print(u.description)
This code has not been tested, and does not writes the CSV, but it should help you getting past that error you were having. The size of 300 for the chunks is completely arbitrary, adjust it if it is too small (or too big).

Twitter API Get User ID from Status ID

I am creating a Twitter bot that will follow the creator of a given status.
I have the status ID (tweet ID), but I need to grab the user ID of the user who posted the tweet in order to follow them. How can I get this? I am using the Twyton package.
You should use the request statuses/show/:id as specified in the Twitter REST API
In Twython, you should call show_status like this:
from twython import Twython
# define application keys here
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
twitter = Twython(consumer_key, consumer_secret, access_token, access_token_secret)
status = twitter.show_status(id='tweet_id')
print status['user']['id_str']

how to post anything else than strings with tweepy

I'm trying to update my Twitter status with generated text. So far generating the text works well, but I cannot post to Twitter.
import tweepy
import SentenceGenerator
with open('Ratschlaege.txt','r') as textfile: #load the text to analyze
sample_text = textfile.read()
#Generating one sentence:
#print SentenceGenerator.generate_sentence(sample_text)
#Generating a paragraph:
sentences = 1
print ' '.join([SentenceGenerator.generate_sentence(sample_text) for i in xrange(sentences)])
# Consumer keys and access tokens, used for OAuth
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
# Sample method, used to update a status
api.update_status(' '.join([SentenceGenerator.generate_sentence(sample_text) for i in xrange(sentences)]))
As far as I have found you can either post strings or from a file, but not the new generated text.
Is there any way?
——EDIT——
okay i tried to build a function inside my script like this
def tweet (tweet):
tweet = ' '.join([SentenceGenerator.generate_sentence(sample_text) for i in xrange(sentences)])
# Consumer keys and access tokens, used for OAuth
consumer_key = 'xxx'
consumer_secret = 'xxx'
access_token = 'xxx'
access_token_secret = 'xxx'
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Creation of the actual interface, using authentication
api = tweepy.API(auth)
# Sample method, used to update a status
api.update_status(tweet)
time.sleep(10)
now it runs, every ten seconds in a loop (to test it out), and there is no error displayed but the status won't update. it runs like its supposed to do, only it doesn't tweet.
what am i missing??
——EDIT——
i also added a part that should insert the generated text into a MySQL db
db = MySQLdb.connect ( host ='127.0.0.1',
user = 'Daniel',
passwd = 'localhost')
cur = db.cursor()
cur.execute("insert into multigram (Ratschlag) value ('%s')" % (tweet))
Same here: No error message but also no entry in the db.
i am sure it's so simple to resolve, but i don't see what i am doing wrong.

Get all friends of a given user on twitter with tweepy

Using tweepy I am able to return all of my friends using a cursor. Is it possible to specify another user and get all of their friends?
user = api.get_user('myTwitter')
print "Retreiving friends for", user.screen_name
for friend in tweepy.Cursor(api.friends).items():
print "\n", friend.screen_name
Which prints a list of all my friends, however if I change the first line
to another twitter user it still returns my friends. How can I get friends for any given user using tweepy?
#first line is changed to
user = api.get_user('otherUsername') #still returns my friends
Additionally user.screen_name when printed WILL return otherUsername
The question Get All Follower IDs in Twitter by Tweepy does essentially what I am looking for however it returns only a count of ID's. If I remove the len() function I will I can iterate through a list of user IDs, but is it possible to get screen names #twitter,#stackoverflow, #etc.....?
You can use the ids variable from the answer you referenced in the other answer to get the the id of the followers of a given person, and extend it to get the screen names of all of the followers using Tweepy's api.lookup_users method:
import time
import tweepy
auth = tweepy.OAuthHandler(..., ...)
auth.set_access_token(..., ...)
api = tweepy.API(auth)
ids = []
for page in tweepy.Cursor(api.followers_ids, screen_name="McDonalds").pages():
ids.extend(page)
time.sleep(60)
screen_names = [user.screen_name for user in api.lookup_users(user_ids=ids)]
You can use this:
# import the module
import tweepy
# assign the values accordingly
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
# authorization of consumer key and consumer secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# set access to user's access key and access secret
auth.set_access_token(access_token, access_token_secret)
# calling the api
api = tweepy.API(auth)
# the screen_name of the targeted user
screen_name = "TwitterIndia"
# printing the latest 20 friends of the user
for friend in api.friends(screen_name):
print(friend.screen_name)
for more details see https://www.geeksforgeeks.org/python-api-friends-in-tweepy/

Categories