i am using following code to search for tweets about an event
import json
import tweepy
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)
for tweet in tweepy.Cursor(api.search, q="manchesterarena", lang="en").items():
print(tweet)
My output looks like:
Status(in_reply_to_user_id_str=None, coordinates=None, retweeted=False, in_reply_to_user_id=None, retweeted_status=Status(in_reply_to_user_id_str=None, coordinates=None, retweeted=False, in_reply_to_user_id=None, created_at=datetime.datetime(2017, 5, 29, 9, 29, 55), contributors=None, possibly_sensitive=False..............
How can i get the tweets as a dictionary or in json format?
Use the ._json property of the tweet object:
for tweet in tweepy.Cursor(api.search, q="manchesterarena", lang="en").items():
print(tweet._json)
You are also currently likely to run up against rate limits - if you initialise your API as:
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
Tweepy will be able to more gracefully handle the rate limits.
Related
I am getting error : help me I tried many times. But it's not showing id from username.
for tweet in tweepy.Cursor(api..........:
try:
screen_name= "NFTfemmefatale"
id = screen_name
get = api.get_user(id)
print("id:" + str (get))
except:
print("error")
try this:
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 user
screen_name = "yourname"
# fetching the user
user = api.get_user(screen_name)
# fetching the ID
ID = user.id_str
print("The ID of the user is : " + ID)
I'm trying to get status updates from a list in Twitter, then open in a CSV file, but I keep getting the following error:
AttributeError: 'Status' object has no attribute 'screen_name'.
Any suggestions?
import tweepy
from tweepy import OAuthHandler
import csv
import pandas as pd
consumer_key = 'x'
consumer_secret = 'x'
access_token = 'x'
access_secret = 'x'
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_secret)
api = tweepy.API(auth)
f = csv.writer(open('TodaysNews.csv', 'w'))
f.writerow(["screenName", "name", "text", "followers_count", "listed_count", "statuses_count"])
number_of_tweets = 100
tweets_for_csv = []
for tweet in tweepy.Cursor(api.list_timeline, 'darrenmeritz', 'News',
twtHandle = tweet.screen_name,
name = tweet.name,
text = tweet.text,
followers_count = tweet.followers_count,
listed_count = tweet.listed_count,
statuses_count = tweet.statuses_count,
result_type='recent',
include_entities=True,
trim_user=True,
truncated=False,
lang='en').items(number_of_tweets):
try:
f.writerow([twtHandle, name, text, followers_count, listed_count, statuses_count])
except UnicodeEncodeError:
pass
With the python code below I tried to fetch 3200 tweets from a public twitter profile, but so far I only get different amounts of tweets which are way less than the maximum of 3200 tweets and I can't really understand the problem. Can someone please explain me what I am doing wrong here?
import tweepy
import json
import pandas as pd
consumer_key = "xxx"
consumer_secret = "xxx"
access_token = "xxx"
access_token_secret = "xxx"
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)
results=[]
timeline = tweepy.Cursor(api.user_timeline, screen_name='#realDonaldTrump', tweet_mode="extended").items()
for status in timeline:
data = (
status.user.id,
status.user.screen_name,
status.user.name,
status.full_text,
status.created_at,
status.lang)
results.append(data)
cols = "user_id screen_name name text date lang".split()
df = pd.DataFrame(results, columns=cols)
I wrote a code to pick friends for a list of ids in twitter.
But API issues make this code very slow.
It's possible improve?
My code is:
import tweepy
consumer_key = ''
consumer_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
for cara in fin1:
if cara in dici1.keys(): next
else:
amigos=[]
for user in tweepy.Cursor(api.friends, screen_name=cara).items():
time.sleep(60)
try:
amigos.append(user.screen_name)
comum = [pessoa for pessoa in amigos if pessoa in fin1]
dici = {cara : comum}
dici1.update(dici)
except: time.sleep(60), next
fin1 is the list of ids(name of the user, 39 in total)
dici1 is a dict is where i store the information.
Remove the time.sleep call, it's not necessary, you also have some stuff that make no sense at all, like those next
import tweepy
consumer_key = ''
consumer_key_secret = ''
access_token = ''
access_token_secret = ''
auth = tweepy.OAuthHandler(consumer_key, consumer_key_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
for cara in fin1:
if cara in dici1
continue
amigos = []
for user in tweepy.Cursor(api.friends, screen_name=cara).items():
try:
amigos.append(user.screen_name)
comum = [pessoa for pessoa in amigos if pessoa in fin1]
dici1[cara] = comum
except:
time.sleep(60)
I was able to get the number of 75,000. After that, It keeps on pulling duplicate IDs. Here is my code. Can I get any suggestions so that I can correctly pull the large amount of follower Ids without duplicates?
import tweepy
import time
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)
ids = []
while True:
try:
for page in tweepy.Cursor(api.followers_ids, screen_name = "...").pages():
ids.extend(page)
except tweepy.TweepError:
time.sleep(60*15)
continue
except StopIteration:
pass
break
I don't know why you are getting duplicates, but you could put the values into a set rather than a list to remove them efficiently.
Just change ids = [] to ids = set()
and ids.extend(page) to ids.update(page)