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')])
Related
I've pulled this code together from a few other posts on here focusing on the topic of searching for Tweets in a certain geographical area. Unfortunately, all I receive from this code is a blank spreadsheet. I have tried a few different iterations with additional parameters added to no avail. Is there something I am missing here?
import tweepy
import csv
consumer_key = 'XXXXX'
consumer_secret = 'XXXXX'
access_token = 'XXXXX'
access_token_secret = 'XXXXX'
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)
search_area = tweepy.Cursor(api.search, count=100, geocode="37.5407,77.4360,5km").items()
output = [[tweet.id_str, tweet.created_at, tweet.text.encode("utf-8"), tweet.favorite_count, tweet.retweet_count,
tweet.entities.get('hashtags'), tweet.entities.get('user_mentions'), tweet.entities.get('media'),
tweet.entities.get('urls')] for tweet in search_area]
with open('city_tweets.csv', 'w') as f:
writer = csv.writer(f)
writer.writerow(["id", "created_at", "text", "likes", "retweets", "hashtags",
"user mentions", "media", "links"])
writer.writerows(output)
37.5407, 77.4360 is 37°32'26.5"N 77°26'09.6"E, which is in a relatively unpopulated area in Western China, where Twitter is blocked, so it makes sense for there to be no Tweets from there in the past week.
Did you mean 37.5407, -77.4360?, which is pretty much the center of Richmond, Virginia.
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)
I have specified to extract tweets since a specified date but I also need to extract tweets before a specified date. The since keyword is used to extract tweets since the given date. SO there must be a keyword which extracts tweets before the specified date. What is that keyword and how to use it?
import tweepy
import csv
import pandas as pd
####input your credentials here
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,wait_on_rate_limit=True)
csvFile = open('demon4.csv', 'a')
csvWriter = csv.writer(csvFile)
for tweet in tweepy.Cursor(api.search,q="#unitedAIRLINES",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')])
In the "q" parameter you can use "since" and "until" like this :
q="#unitedAIRLINES since:2017-04-02 until:2017-04-03"
The result shoud be the same as this advanced search on the official web site :
https://twitter.com/search?f=tweets&vertical=default&q=%23unitedAIRLINES%20since%3A2017-04-02%20until%3A2017-04-03&src=typd
Except that with the public search API you just can get 7 days past.
You can either use a specific tweet id as a starting point. The parameter is "since_id". And a "max_id" to delimit the period. For more information, see : https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets.html
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()
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()