How to retrieve/find with specific text on tweet from user timeline twitter with python

Nanda

I'm trying to get specific tweet from user timeline with contain text "#Gempa" using python

i am able to get user timeline, but i want to get the timeline with text just contain "#Gempa" or something specific text

here is my code

#Import the necessary methods from tweepy library
import tweepy, codecs
import pymysql
import time

#Variables that contains the user credentials to access Twitter API 
access_token = "XXX"
access_token_secret = "XXX"
consumer_key = "XXX"
consumer_secret = "XXX"

#Authentication
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

#Declare Connection
conn = pymysql.connect(host='localhost', port='', user='root', passwd='', db='test', use_unicode=True, charset="utf8mb4")
cur = conn.cursor()

#Get Current Date Time
curdatetime = time.strftime("%Y-%m-%d %H:%M:%S")

cur.execute("DELETE FROM tweet order by id desc LIMIT 500")

#Get last id from table tweet
last_id = 0
cur.execute("SELECT MAX(id) FROM tweet")
result = cur.fetchall()
for row in result:
    last_id = row[0]
    print ("Last ID : " + str(last_id))

#Get Number of Tweet
user = api.get_user(108543358)
print ("Name:", user.name)
print ("Name:", user.screen_name)
print ("Number of tweets: " + str(user.statuses_count))
print ("followers_count: " + str(user.followers_count))
print ("Account location: ", user.location)
print ("Account created at: ", user.created_at)


n = 0
for Tweet in tweepy.Cursor(api.user_timeline, id=108543358, q = "#Gempa", lang = id, result_type = "recent", since_id = last_id).items(3):
    print ("*****" + str(i) +"*****")
    print ("ID: " + Tweet.id_str)
    print ("Text: " + str(Tweet.text.encode("utf-8")))
    print ("Retweet Count: " + str(Tweet.retweet_count))
    print ("Favorite Count: " + str(Tweet.favorite_count))
    print ("Date Time: " + str(Tweet.created_at))
    #print (str(Tweet.location)) #how to get geolocation data for mapping ?
    print ("************")

    n = n + 1
    cur.execute("INSERT INTO tweet (no, id, text, retweet_count, favourite_count, date_time) VALUES (%s, %s,%s,%s,%s,%s)",
    (str(n), Tweet.id_str, Tweet.text.encode("utf-8"), str(Tweet.retweet_count), str(Tweet.favorite_count), str(Tweet.created_at)))


conn.commit()
cur.close()
conn.close()

and the result is

result

i can't get user timeline with specific text, anyone can solve this, thanks before

JeffProd

First of all there are no "q", "lang", "result_type" parameters for API.user_timeline (read http://docs.tweepy.org/en/v3.5.0/api.html#API.user_timeline)

So to ignore some tweets, you have to code a filter. You just can skip tweets not containing "#Gempa" like this :

for Tweet in api.user_timeline(user_id=108543358):
    text = str(Tweet.text.encode("utf-8"))
    if "#Gempa" not in text:
        continue
    print ("*****" + str(n) +"*****")
    print ("ID: " + Tweet.id_str)
    ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting full tweet text from "user_timeline" with tweepy

Twitter - How to embed native video from someone else's tweet into a New Tweet or a DM

Getting whole user timeline of a Twitter user

How to get only Tweet (only posts) from the timeline?

Is it possible to read tweet-text of a tweet URL without twitter API?

Get Historical Tweet Metadata from Twitter API in Python

Get a Twitter user timeline

How to print tweet from from specific profile in python using twitter api

Get latest tweet from user_timeline with twit npm

How can I get a Twitter public timeline with no user authentication using Twitter4j?

How to get more than 20 results from a user timeline using processing and Twitter4j?

How to get twitter user_timeline using Mono?

How to prevent a callback in the Twitter stream from firing with each new tweet?

How to remove tweet photos from twitter widget?

How to get other user's mention timeline of twitter with twitter4j api?

Twitter user timeline in ListView by Fabric

How to check the text of a tweet for a specific keyword from an array in python

How to get tweet text from Adapters?

How to know the number of photos in a Tweet from Twitter API Java JSON

Ruby: How to pass options to user_timeline method of twitter gem?

Python Twitter Streaming Timeline

how to insert any twitter user profile link while composing a tweet

how to get the full tweet of the user timeline with twitter4j?

Twitter4j - quote tweet with full 280 characters for user text without url on quoted tweet

Has a user seen a certain tweet? Twitter API

Twitter - i only recieve 20 tweets from the user_timeline

Specific Twitter users not returning tweets with user_timeline()

Twitter API how to get the latest tweet | Python

How to get source video URL from user tweet timeline using twitter API V2

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive