I want to develop a Jupyter Notebook which on executing every time shows the top 10 Twitter trending topics in India in the last 24 hours.
I got everything set up
auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)
and when I run trends1 = api.trends_place(23424848), it gives a AttributeError: 'API' object has no attribute 'trends_place'
And if this attribute has been removed then what should I do to get my work done Please help..
You are getting this error because api do not have this trends_place attribute.
If u check the documentation (https://docs.tweepy.org/en/stable/api.html#trends), you will see that instead of using api.trends_place(), the correct syntax is api. followed by the attribute get_place_trends().
So, i suggest the following code to get the desired result:
auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)
WOEID = 23424848
top_trends = api.get_place_trends(WOEID)
Note that top_trends is an dictionary inside a list of length 1. Treat it like top_trends[0]['trends'] and so on to get specific values. Example:
top_trends[0]['trends'][0]['name']
top_trends[0]['trends'][0]['url']
top_trends[0]['trends'][0]['promoted_content']
top_trends[0]['trends'][0]['query']
top_trends[0]['trends'][0]['tweet_volume']
To get trending topics near a specific location on Twitter,
Once you've set up:
auth = tweepy.OAuthHandler(apikey,apisecretkey)
auth.set_access_token(accesskey,accesssecret)
api = tweepy.API(auth)
use trends = api.trends_place(WOEID) to get the 50 trending topics based on the lWOEID
I'm trying to search for tweets containing a specified hashtag, and then get the user that created the tweet.
import tweepy
tweets = tweepy.Cursor(api.search, q="python", tweet_mode='extended').items()
while True:
tweet = next(tweets)
for hashtag in tweet.entities.get('hashtags'):
ht = hashtag['text']
# process the hashtag...
# who is the user that created this tweet?
user = ???
How do I go about extracting the user from the tweet object?
You can look at either the name property or screen_name property of the user object to find out who posted a tweet.
print(tweet.user.name)
print(tweet.user.screen_name)
For example, my name on Twitter is Bill the Lizard, but by screen_name is #lizardbill.
I'm trying to write a program that will stream tweets from Twitter using their Stream API and Tweepy. Here's the relevant part of my code:
def on_data(self, data):
if data.user.id == "25073877" or data.in_reply_to_user_id == "25073877":
self.filename = trump.csv
elif data.user.id == "30354991" or data.in_reply_to_user_id == "30354991":
self.filename = harris.csv
if not 'RT #' in data.text:
csvFile = open(self.filename, 'a')
csvWriter = csv.write(csvFile)
print(data.text)
try:
csvWriter.writerow([data.text, data.created_at, data.user.id, data.user.screen_name, data.in_reply_to_status_id])
except:
pass
def on_error(self, status_code):
if status_code == 420:
return False
What the code should be doing is streaming the tweets and writing the text of the tweet, the creation date, the user ID of the tweeter, their screen name, and the reply ID of the status they're replying to if the tweet is a reply. However, I get the following error:
File "test.py", line 13, in on_data
if data.user.id == "25073877" or data.in_reply_to_user_id == "25073877":
AttributeError: 'unicode' object has no attribute 'user'
Could someone help me out? Thanks!
EDIT: Sample of what is being read into "data"
{"created_at":"Fri Feb 15 20:50:46 +0000 2019","id":1096512164347760651,"id_str":"1096512164347760651","text":"#realDonaldTrump \nhttps:\/\/t.co\/NPwSuJ6V2M","source":"\u003ca href=\"http:\/\/twitter.com\" rel=\"nofollow\"\u003eTwitter Web Client\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":25073877,"in_reply_to_user_id_str":"25073877","in_reply_to_screen_name":"realDonaldTrump","user":{"id":1050189031743598592,"id_str":"1050189031743598592","name":"Lauren","screen_name":"switcherooskido","location":"United States","url":null,"description":"Concerned citizen of the USA who would like to see Integrity restored in the US Government. Anti-marxist!\nSigma, INTP\/J\nREJECT PC and Identity Politics #WWG1WGA","translator_type":"none","protected":false,"verified":false,"followers_count":1459,"friends_count":1906,"listed_count":0,"favourites_count":5311,"statuses_count":8946,"created_at":"Thu Oct 11 00:59:11 +0000 2018","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"FF691F","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/1068591478329495558\/ng_tNAXx_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/1068591478329495558\/ng_tNAXx_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1050189031743598592\/1541441602","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"quote_count":0,"reply_count":0,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"urls":[{"url":"https:\/\/t.co\/NPwSuJ6V2M","expanded_url":"https:\/\/www.conservativereview.com\/news\/5-insane-provisions-amnesty-omnibus-bill\/","display_url":"conservativereview.com\/news\/5-insane-\u2026","indices":[18,41]}],"user_mentions":[{"screen_name":"realDonaldTrump","name":"Donald J. Trump","id":25073877,"id_str":"25073877","indices":[0,16]}],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"und","timestamp_ms":"1550263846848"}
So I supposed the revised question is how to tell the program to only write parts of this JSON output to the CSV file? I've been using the references Twitter's stream API provides for the attributes for "data".
As stated in your comment the tweet data is in "JSON format". I believe what you mean by this is that it is a string (unicode) in JSON format, not a parsed JSON object. In order to access the fields like you want to in your code you need to parse the data string using json.
e.g.
import json
json_data_object = json.loads(data)
you can then access the fields like you would a dictionary e.g.
json_data_object['some_key']['some_other_key']
This is a very late answer, but I'm answering here because this is the first search hit when you search for this error. I was also using Tweepy and found that the JSON response object had attributes that could not be accessed.
'Response' object has no attribute 'text'
Through lots of tinkering and research, I found that in the loop where you access the Twitter API, using Tweepy, you must specify '.data' in the loop, not within it.
For example:
tweets = client.search_recent_tweets(query = "covid" , tweet.fields = ['text'])
for tweet in tweets:
print(tweet.text) # or print(tweet.data.text)
Will not work because the Response variable doesn't have access to the attributes within the JSON response object. Instead, you do something like:
tweets = client.search_recent_tweets(query = "covid" , tweet.fields = ['text'])
for tweet in tweets.data:
print(tweet.text)
Basically, this was a long-winded way to fix a problem I was having for a long time. Cheers, hopefully, other noobs like me won't have to struggle as long as I did!
I am trying to mine data in a local area.
First I would like to Twython to search all tweets on twitter to find those tweets that contain a particular keyword, and then print those tweets. I have successfully written the few lines of code that do this:
from twython import Twython
TWITTER_APP_KEY = ''
TWITTER_APP_KEY_SECRET = ''
TWITTER_ACCESS_TOKEN = ''
TWITTER_ACCESS_TOKEN_SECRET = ''
t = Twython(TWITTER_APP_KEY, TWITTER_APP_KEY_SECRET, TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET)
search = t.search(q='hello', count=100)
tweets = search['statuses']
for tweet in tweets:
print (tweet['text'], '\n')
Next, I want to generate the user name of the ID that tweeted the text (not just the ID but the actual username), and print it.
After this, I would like to generate the friends list of the particular user, and print it.
How can I get the user name and the friends list?
To generate the username, you can look in the "user" section of each status. There will be a key called "screen_name" and its value is what you'll need.
In the user section, there is also an "id". You'll need to create a separate call using this id to get that user's followers. The twitter api (and twython) supports getting followers from an id. See https://dev.twitter.com/docs/api/1.1/get/followers/ids
I am trying to view another user's tweets. The other user is following me and i am following the user on twitter. But when i try this, i only see my own tweets, no matter what name i enter as argument for GetUserTimeline.
What should i do??
import twitter
api = twitter.Api(consumer_key='', consumer_secret='', access_token_key='',access_token_secret='')
statuses = api.GetUserTimeline('chooimooi')
for tweet in statuses:
print tweet
Also, how can i export this data to a text file?
Take a look at pydoc for twitter.Api.GetUserTimeline
pydoc twitter.Api.GetUserTimeline
which states:
twitter.Api.GetUserTimeline = GetUserTimeline(self, user_id=None, screen_name=None,
since_id=None, max_id=None, count=None, include_rts=True, trim_user=None,
exclude_replies=None) unbound twitter.Api method
I think therefore that putting screen_name='usernamerequired' will work. For example
statuses = api.GetUserTimeline(screen_name='chooimooi')