Trying to perform what I thought was a very basic exercise. I have a list of 7000+ customers and I want to import some of their attributes from twitter. I am trying to figure out how to input several user names into my tweepy query, but I am not getting any luck with below.
#!/usr/bin/python
import tweepy
import csv #Import csv
import os
# Consumer keys and access tokens, used for OAuth
consumer_key = 'MINE'
consumer_secret = 'MINE'
access_token = 'MINE'
access_token_secret = 'MINE'
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('ID.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
users = ('IBM' or 'massmutual')
user = api.get_user(screen_name = users)
csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.description.encode('utf-8')])
print user.id
csvFile.close()
You will have to do it in a loop, you will make list of users and get user instance for all one by one in loop.
#!/usr/bin/python
import tweepy
import csv #Import csv
import os
# Consumer keys and access tokens, used for OAuth
consumer_key = 'MINE'
consumer_secret = 'MINE'
access_token = 'MINE'
access_token_secret = 'MINE'
# OAuth process, using the keys and tokens
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# Open/Create a file to append data
csvFile = open('ID.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
users = ['IBM','massmutual','user3',.......]
for user_name in users:
user = api.get_user(screen_name = user_name)
csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.description.encode('utf-8')])
print user.id
csvFile.close()
Related
Anyone have any idea what could be causing the error? All my keys and tokens should be correct. I made sure to double check them. I followed the steps on how to set up the stream pretty arcuately I thought.
import time
import tweepy
import praw
#Variables that contains the credentials to access Twitter API and REDDIT
USERNAME = ""
PASSWORD = "!"
CLIENT_ID = ''
CLIENT_SECRET = ''
consumer_key = ''
consumer_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, consumer_key)
api = tweepy.API(auth, wait_on_rate_limit=True)
class MyStreamListener(tweepy.Stream):
def on_status(self, status):
print("ID: {}".format(status.id))
print(status.full_text)
def streamtweets():
myStreamListener = MyStreamListener(consumer_key, consumer_secret,access_token, consumer_key)
myStream = tweepy.Stream(consumer_key, consumer_secret,access_token, consumer_key)
myStream.filter(follow = [''])
streamtweets()
You're passing your consumer_key as the access token secret.
I am trying to get data for tweets which I am able to obtain using the following code. However, I would also like to get the location and original tweets instead of retweets. Please suggest on how to go about it.
import tweepy
import csv
import pandas as pd
####input your credentials here
consumer_key= 'XXXX'
consumer_secret= 'XXXX'
access_token = 'XXXX'
access_token_secret = 'XXXX'
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)
# Open/Create a file to append data
csvFile = open('ua.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,q="#RamMandir",count=100,
lang="en",
since="2017-04-03").items():
print (tweet.created_at, tweet.text)
csvWriter.writerow([tweet.created_at, tweet.text.encode('utf-8')])
I was just wondering if anyone knew how to list out the usernames that a twitter user is following, and their followers in two separate .csv cells.
This is what I have tried so far.
import tweepy
import csv
consumer_key = ""
consumer_secret = ""
access_token = ""
access_token_secret = ""
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
csvFile = open('ID.csv', 'w')
csvWriter = csv.writer(csvFile)
users = ['AindriasMoynih1', 'Fiona_Kildare', 'daracalleary', 'CowenBarry', 'BillyKelleherTD', 'BrendanSmithTD']
for user_name in users:
user = api.get_user(screen_name = user_name, count=200)
csvWriter.writerow([user.screen_name, user.id, user.followers_count, user.followers_id, user.friends_id user.description.encode('utf-8')])
print (user.id)
csvFile.close()
Tweepy is a wrapper around the Twitter API.
According to the Twitter API documentation, you'll need to call the GET friends/ids to get a list of their friends (people they follow), and GET followers/ids to get their followers.
Using the wrapper, you'll invoke those API calls indirectly by calling the corresponding method in Tweepy.
Since there will be a lot of results, you should use the Tweepy Cursor to handle scrolling through the pages of results for you.
Try the code below. I'll leave it to you to handle the CSV aspect, and to apply it to multiple users.
import tweepy
access_token = "1234"
access_token_secret = "1234"
consumer_key = "1234"
consumer_secret = "1234"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
for user in tweepy.Cursor(api.get_friends, screen_name="TechCrunch").items():
print('friend: ' + user.screen_name)
for user in tweepy.Cursor(api.get_followers, screen_name="TechCrunch").items():
print('follower: ' + user.screen_name)
When I run it, the terminal keeps tying "23851" in new rows, which is the number of followers of the first Twitter name in my file f; I believe this means that the pointer was not moving in file f, but I'm not sure how this should be done properly in Python 2) when I check my file f1, there's nothing, i.e. the program is not writing to f1 as expected.
import tweepy
from tweepy import Stream
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_KEY = 'xxx'
ACCESS_SECRET = 'xxx'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
api = tweepy.API(auth)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
#Create Class First
class TweetListener(StreamListener):
# A listener handles tweets are the received from the stream.
#This is a basic listener that just prints received tweets to standard output
def on_data(self, data): # indented inside the class
print(data)
return True
def on_error(self, status):
print(status)
# open both files outside the loop
with open('Twitternames.txt') as f,open('followers_number.txt', 'a') as f1:
for x in f:
#search
api = tweepy.API(auth)
twitterStream = Stream(auth,TweetListener())
test = api.lookup_users(screen_names=['x'])
for user in test:
print(user.followers_count)
#print it out and also write it into a file
s = user.followers_count
f1.write(str(s) +"\n") # add a newline with +
#end of stackoverflow
f.close()
Actually there are some things to consider, There are some unwanted lines as well. So I will go line by line and explain the relevant things ,as we don't need any streaming data for counting the number of follower , so we need to import only tweepy and OauthHandler, so :
import tweepy
from tweepy import OAuthHandler
Now we need to set the 4 keys required for login so, This will go same as :
CONSUMER_KEY = 'xxxxxxxx' #Replace with the original values.
CONSUMER_SECRET = 'xxx' #Replace with the original values.
ACCESS_KEY = 'xxx' #Replace with the original values.
ACCESS_SECRET = 'xxx' #Replace with the original values.
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
I don't guess you would need, StreamListner to just log the follower_count of various users. So I am skipping that part, However you can add that code snippet afterwards.
usernames_file = open('Twitternames.txt').readlines()
I am assuming the contents of Twitternames.txt to be in the following format(every username without # symbol and separated by a new line):
user_name_1
user_name_2
user_name_3
...
now the usernames_file would be list of strings usernames_file= ['user_name_1\n', 'user_name_2\n', 'user_name_3\n'] so now we have extracted the various usernames from the text file, but we need to get rid of that \n character at the end of each name. So we can use .strip() method.
usernames = []
for i in usernames_file:
usernames.append(i.strip())
>>> usernames = ['user_name_1', 'user_name_2', 'user_name_3']
Now we are ready to use the lookup_users method as this method takes a list of usernames as input.
So it may look something like this:
test = api.lookup_users(screen_names=usernames)
for user in test:
print(user.followers_count)
If you want to log the results to a .txt file then you can use:
log_file = open("log.txt", 'a')
test = api.lookup_users(screen_names=usernames)
for user in test:
print(user.followers_count)
log_file.write(user.name+" has "+str(user.followers_count)+" followers.\n")
log_file.close()
So the short and final code would look something like this:
import tweepy
from tweepy import OAuthHandler
CONSUMER_KEY = 'xxx'
CONSUMER_SECRET = 'xxx'
ACCESS_KEY = 'xxx'
ACCESS_SECRET = 'xxx'
auth = OAuthHandler(CONSUMER_KEY,CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)
usernames_file = open('Twitternames.txt').readlines()
usernames = []
for i in usernames_file:
usernames.append(i.strip())
log_file = open("log.txt", 'a')
test = api.lookup_users(screen_names=usernames)
for user in test:
print(user.followers_count)
log_file.write(user.name+" has "+str(user.followers_count)+" followers.\n")
log_file.close()
I've put together this short script to search twitter. The majority of the tweets from this search date back from a year ago. It was in connection with a kickstarter campaign. When I run this script though I only get newer tweets that aren't relevant to that term anymore. Could anybody tell me what I need to do to get it the way I want? When I search for the terms on twitter it gives me the right results.
import tweepy
import csv
consumer_key = 'x'
consumer_secret = 'x'
access_token = 'x'
access_token_secret = 'x'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
# Open/Create a file to append data
csvFile = open('tweets.csv', 'a')
#Use csv Writer
csvWriter = csv.writer(csvFile)
api = tweepy.API(auth)
results = api.search(q="kickstarter campaign")
for result in results:
csvWriter.writerow([result.created_at, result.text.encode('utf-8')])
print result.created_at, result.text
csvFile.close()