How to get the last tweets of an account - python

Im working on a script that is looking for the last tweets of a twitter user.
im using tweepy and I know there is this answer which works
Get the last tweet with tweepy
however it also prints the replies of a user.
And i would like to have only the actual tweets a person is sending out
tweetL = api.user_timeline(screen_name='elonmusk', tweet_mode="extended")
print(tweetL[0].full_text)
How can i do this is there a function for that?

You can pass an extra parameter to the user_timeline function, it's called exclude_replies.
Please checkout the documentation to know all the posible parameters of that function.

Related

Can we use the "SInce" and "Until" option in TWEEPY to fetch tweets from a specific date?

Actually, I am working on a project which collects tweets if we pass a certain keyword. For ex. If I pass the keyword as "Messi", it will collect every tweets regarding Messi. We are passing the parameters as "query" and "no of tweets". No of tweets will restrict the count of tweets that we need. So, tweepy collects the recent tweets from the field. What I want, is that I want to retrieve tweets from a certain timeline. Suppose, I want the tweets regarding "Messi" from 20th Jan 2022 to 02nd Feb 2022, it should fetch from that certain timeline.I've used POSTMAN with the twitter API and I am getting the results in that, but it is not being applied in the Python Tweepy code. So, do we have any option regarding that?
I tried POSTMAN, and in that we have the endpoint of "Full Archived Search". So, we can pass the since and until option in that, but it is not being applied to the Python Tweepy Code, which I'm doing. So, can we apply since and until option in the tweepy code? If not, do we have any ther alternative for it?

Is there a way to filter out Retweets from results of search_tweets using Tweepy?

I'm using Tweepy to query Tweets with certain keywords and am able to a get a list of Tweet objects (or Status objects?) (https://developer.twitter.com/en/docs/twitter-api/data-dictionary/object-model/tweet).
I'm wondering if there's a way to remove Retweets from all the Tweets since I want to look at original Tweets and replies specifically. If there's an attribute in the Tweet object, that'd solve my problem but didn't figure that out so far.
Thanks in advance!
If you're using the standard search API with API.search_tweets, you can use the -filter:retweets operator in your query.
Also, if you're using API.search_tweets, you're using Twitter API v1.1, and https://developer.twitter.com/en/docs/twitter-api/v1/data-dictionary/object-model/tweet is the corresponding documentation for the Tweet object. You can distinguish Retweets by the existence of a retweeted_status attribute.

How to keyword search only tweets from 'People you Follow' in Tweepy

Is it possible to use the API search in Tweepy to restrict a keyword search to only people you follow? For example, on the web, the URL would be: https://twitter.com/search?q=keyword&f=live&pf=on
I tried using the pf= paramater, since that is what shows up in the web URL. For example: pf='on' or pf=true. But it is still returning unfiltered tweets.
What I have currently:
for tweet in tweepy.Cursor(api.search,
q='keyword',
result_type='recent',
pf='on').items(20)
I don't see any reference to this in the API docs. Is there some other it would have to be done?
There's nothing available in the search API that would let you do this using an operator directly. One (clunky) way you could achieve this would be to put all the the people you are following onto a Twitter list, and then use the list:username/listname operator inside the q parameter to limit the search to the accounts in that list.

Twitter API - Obtain user tweets and parse into a table/database

This is a small project I'd like to get started on in the near future. It's still in the planning stage so this post is more about being steered in the right direction
Essentially, I'd like to obtain tweets from a user and parse the tweets into a table/database, with the aim to be able to run this program in real-time.
My initial plan to tackle this was to use Beautiful Soup, a Python specific library, however, I believe the Twitter API is the better approach (advice on this subject would be appreciated)
There are still 3 unknowns:
Where do I store the tweets once obtained?
How to parse the tweets?
Where to store the parsed data?
To answer (3), I suppose it depends on what I want to do with the data. I still haven't decided how I'll use the parsed data but I know that I'd like it put into categories so my thinking is probably a database/table/excel??
A few questions still to answer and I'd like you guys to steer me in the right direction. My programming language knowledge is limited to just C for now, but as this project means a great deal to me, I'm willing to put the effort in and learn the necessary languages/APIs.
What languages/APIs will I need to gain an understanding of to accomplish this project? From where I stand, it seems to be Twitter API and Python.
EDIT: So I have a basic script going which obtains a user tweets. It works better than expected. However, I'd like to take it another step. I'd like to only obtain the users' tweets if it contains a hashtag inside of the tweet. All other tweets should be ignored. How best to do this?
Here is a snippet of the basic code I have going:
import tweepy
import twitter_credentials
auth = tweepy.OAuthHandler(twitter_credentials.CONSUMER_KEY, twitter_credentials.CONSUMER_SECRET)
auth.set_access_token(twitter_credentials.ACCESS_TOKEN, twitter_credentials.ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
stuff = api.user_timeline(screen_name = 'XXXXXXXXXX', count = 10, include_rts = False)
for status in stuff:
print(status.text)
Scraping Twitter (or any other social network) with for example Beautiful soup, as you said, is not a good idea for 2 reasons :
if the source pages changes (name attributes, div ids...), you have to keep your code up to date
your script can be banned because scraping is not "allowed".
To answer your questions :
1) you can store the tweets wherever you want : csv, mysql, sqlite, redis, neo4j...
2) With official API, you get JSON. Here is a Tweet Object : https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/tweet-object.html . With tweepy, for example status.text will give you the text of the tweet.
3) Same as #1. If you don't know actually what you will do with the data, store the full JSONs. You will be able later to parse them.
I suggest tweepy/python (http://www.tweepy.org/) or twit/nodejs (https://www.npmjs.com/package/twit). And read official docs : https://developer.twitter.com/en/docs/api-reference-index

Twitter Streaming without Keyword

I'm trying to stream Twitter using Tweepy and I was wondering if it is possible to stream without giving a keyword? Therefore I would be able to stream all tweets instead of only ones with a given keyword. The code I'm following can be found here: https://gist.github.com/bonzanini/af0463b927433c73784d Someone commented saying that this isn't possible and I just wanted to double check that this is true or if there is a work around without having to buy the Firehose.
You can access a sample of all tweets by using this endpoint
https://dev.twitter.com/streaming/reference/get/statuses/sample
As is made clear in the documentation:
Returns a small random sample of all public statuses.
If you want all tweets then, yes, you need to pay for access.

Categories

Resources