tweepy search by user + query - python

I want to search the whole timeline of a specific user for a particular query
I tried setting the .items() to the total number of tweets in the user profile
but I'm not getting any results back .. any idea on how to solve this issue?
username="No_Rumors"
t =" from:No_Rumors"
z= "أمين رابطة العالم الإسلامي يقرع جرس الكنيسة النصرانية"
no_of_tweets = api.get_user(screen_name=username).statuses_count
query=z+t
t = tweepy.Cursor(api.search,q=query).items(no_of_tweets)
for tweet in t:
print(tweet.text)

Related

How to search a specific country's tweets with Tweepy client.search_recent_tweets()

y'all. I'm trying to figure out how to sort for a specific country's tweets using search_recent_tweets. I take a country name as input, use pycountry to get the 2-character country code, and then I can either put some sort of location filter in my query or in search_recent_tweets params. Nothing I have tried so far in either has worked.
######
import tweepy
from tweepy import OAuthHandler
from tweepy import API
import pycountry as pyc
# upload token
BEARER_TOKEN='XXXXXXXXX'
# get tweets
client = tweepy.Client(bearer_token=BEARER_TOKEN)
# TAKE USER INPUT
countryQuery = input("Find recent tweets about travel in a certain country (input country name): ")
keyword = 'women safe' # gets tweets containing women and safe for that country (safe will catch safety)
# get country code to plug in as param in search_recent_tweets
country_code = str(pyc.countries.search_fuzzy(countryQuery)[0].alpha_2)
# get 100 recent tweets containing keywords and from location = countryQuery
query = str(keyword+' place_country='+str(countryQuery)+' -is:retweet') # search for keyword and no retweets
posts = client.search_recent_tweets(query=query, max_results=100, tweet_fields=['id', 'text', 'entities', 'author_id'])
# expansions=geo.place_id, place.fields=[country_code],
# filter posts to remove retweets
# export tweets to json
import json
with open('twitter.json', 'w') as fp:
for tweet in posts.data:
json.dump(tweet.data, fp)
fp.write('\n')
print("* " + str(tweet.text))
I have tried variations of:
query = str(keyword+' -is:retweet') # search for keyword and no retweets
posts = client.search_recent_tweets(query=query, place_fields=[str(countryQuery), country_code], max_results=100, tweet_fields=['id', 'text', 'entities', 'author_id'])
and:
query = str(keyword+' place.fields='+str(countryQuery)+','+country_code+' -is:retweet') # search for keyword and no retweets
posts = client.search_recent_tweets(query=query, max_results=100, tweet_fields=['id', 'text', 'entities', 'author_id'])
These either ended up pulling me NoneType tweets aka nothing or causing a
"The place.fields query parameter value [Germany] is not one of [contained_within,country,country_code,full_name,geo,id,name,place_type]"
The documentation for search_recent_tweets seems like place.fields / place_fields / place_country should be supported.
Any advice would help!!!

Twitter API to fetch Tweets in Python

I'm trying to fetch Tweets from multiple Twitter accounts and then create a database with the TWEETS and the source of the TWEET " user name " by using the following code
posts = api.user_timeline(screen_name = 'AlArabiya_Brk', count = 100 , lang =
"ar",tweet_mode="extended")
df = pd.DataFrame([tweet.full_text for tweet in posts], columns = [ 'Tweets'])
but I have a question: how can I add more than one account? I tried doing:
posts = api.user_timeline(screen_name = ['AlArabiya_Brk','AJABreaking'], count = 100
,lang ="ar",tweet_mode="extended")
but didn't get the desired output
You'll need to make multiple calls with that method.
That API endpoint only allows a single screen name input.

How to get followers and followee list of >500 Twitter users using tweepy?

I want to create a network graph of followers and followee of a list of over 500 Twitter users and for that, I am storing followers list and followee list of every user in user_list. I tried this code but it took too long to execute (it's been >18hrs) and at the end gave an error that I pasted here: https://pastebin.com/dgBgGN6M
user_list = []
for user in unique_creators:
ff_list = []
followers = []
for follower in tweepy.Cursor(api.followers, screen_name=user, count=1000).items():
followers.append(follower.screen_name)
friends = []
for friend in tweepy.Cursor(api.friends, screen_name=user, count=1000).items():
friends.append(friend.screen_name)
ff_list = [followers, friends]
user_list.append(ff_list)
Here unique_creators is the list of screen_names of Twitter users. Can someone please tell if I am doing something wrong?

TweePy For Loop not looping all data

Use this is code snippet :
user = api.get_user('xxxx')
print(user.followers_count)
for i in user.friends():
print(i.screen_name)
result for follower count is 700
but why when looping all data only show 21 row data?
Whilst getting the names of all followers of a user is possible you are likely to get banned from exceeding rate limits quickly.
The following code will tell you how many followers a user has and then list all the username of the people who follow the user.
username = 'keely_bro'
userID = api.get_user(username)
print('User is followed by ' + str(userID.followers_count) + ' people')
print('usernames of followers: ')
for allFollowers in tweepy.Cursor(api.followers_ids, screen_name=username).pages():
followerNames = [userID.screen_name for userID in api.lookup_users(allFollowers)]
for follow in followerNames:
print(follow)

Tweepy api.followers and count limits

I'm looking to retrieve the followers on some account with more than 5000 followers.
I've seen in a other topic than it's easier to proceed by page then by items per page. (link : tweepy count limited to 200?)
Once it reads all the id, it must check on the profile description if there is a element of a list i created before.
here's my previous code (note op, without using the api.followers):
for element in liste2:
print element
resultats = api.search_users(q=element, count=5000)
for user in resultats:
print user.id
i = 0;
user = api.get_user(user.id)
print user.name
while (i != 4):
if (user.description.find(liste1[i])!= 1):
print user.name + " valide"
i = 4;
statuses = api.user_timeline(id = user.id, count = 20\
0)
The counter doesn't work that's why i want to switch for api.followers witch seems to be more nice.
Thanks for reading

Categories

Resources