Search code examples
pythontweepy

How to search trends using tweepy?


I'm coding a bot, and I need to search up the trends that I record. But tthe search result that returns is just [], aka, nothing. Here's a pastebin of the code. https://pastebin.com/pkJ2McUq And a code snippet.

import tweepy
APIKey=input("API Key, Please.\n")
APIKeysecret=input("And To Confirm, Your Secret Api Key.\n")
AccessToken=input("Your Access Token?\n")
AccessTokenSecret=input("And The Secret Token.\n")
auth = tweepy.OAuthHandler(APIKey, APIKeysecret)
auth.set_access_token(AccessToken, AccessTokenSecret)
with open("TrendsResult/atrendsresults.json", 'w') as atrendssearchresults :
   print(api.search('IndividualTrends/0.txt', lang="EN", result_type = "popular"), file=atrendssearchresults)
 with open("TrendsResult/btrendsresults.json", 'w') as btrendssearchresults :
   print(api.search('IndividualTrends/1.txt', lang="EN", result_type = "popular"), file=btrendssearchresults)
 with open("TrendsResult/ctrendsresults.json", 'w') as ctrendssearchresults :
   print(api.search('IndividualTrends/2.txt', lang="EN", result_type = "popular"), file=ctrendssearchresults)  
 with open("TrendsResult/dtrendsresults.json", 'w') as dtrendssearchresults :
   print(api.search('IndividualTrends/3.txt', lang="EN", result_type = "popular"), file=dtrendssearchresults)  
 with open("TrendsResult/etrendsresults.json", 'w') as etrendssearchresults :
   print(api.search('IndividualTrends/4.txt', lang="EN", result_type = "popular"), file=etrendssearchresults)  
 with open("TrendsResult/ftrendsresults.json", 'w') as ftrendssearchresults :
   print(api.search('IndividualTrends/5.txt', lang="EN", result_type = "popular"), file=ftrendssearchresults)  
 with open("TrendsResult/gtrendsresults.json", 'w') as gtrendssearchresults :
   print(api.search('IndividualTrends/6.txt', lang="EN", result_type = "popular"), file=gtrendssearchresults)
 with open("TrendsResult/htrendsresults.json", 'w') as htrendssearchresults :
   print(api.search('IndividualTrends/7.txt', lang="EN", result_type = "popular"), file=htrendssearchresults)    
 with open("TrendsResult/itrendsresults.json", 'w') as itrendssearchresults :
   print(api.search('IndividualTrends/8.txt', lang="EN", result_type = "popular"), file=itrendssearchresults)
 with open("TrendsResult/jtrendsresults.json", 'w') as jtrendssearchresults :
   print(api.search('IndividualTrends/j.txt', lang="EN", result_type = "popular"), file=jtrendssearchresults)

How do I fix it?


Solution

  • None of those searches will return any results because there are no Tweets that match those queries.
    The first parameter for API.search is the search query string, and you're searching for the strings themselves instead of whatever is in the files that they reference.
    Also, you should look into using loops.