【问题标题】:How to search trends using tweepy?如何使用 tweepy 搜索趋势?
【发布时间】:2019-12-17 23:41:03
【问题描述】:

我正在编写一个机器人,我需要搜索我记录的趋势。但是返回的搜索结果只是[],也就是什么都没有。这是代码的粘贴箱。 https://pastebin.com/pkJ2McUq 和一个代码 sn-p。

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)

我该如何解决?

【问题讨论】:

    标签: python tweepy


    【解决方案1】:

    这些搜索都不会返回任何结果,因为没有与这些查询匹配的推文。
    API.search 的第一个参数是搜索查询字符串,您搜索的是字符串本身,而不是它们引用的文件中的任何内容。
    此外,您应该考虑使用循环。

    【讨论】:

      【解决方案2】:

      试试这个:

      # -*- coding: utf-8 -*-
      import sys
      import tweepy
      import json
      
      #Autenticações
      consumer_key = ''
      consumer_secret = ''
      access_token = ''
      access_token_secret = ''
      
      auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
      auth.set_access_token(access_token, access_token_secret)
      api = tweepy.API(auth)
      
      # Where On Earth ID for Brazil is 23424768.
      BRAZIL_WOE_ID = 23424768
      
      brazil_trends = api.trends_place(BRAZIL_WOE_ID)
      
      trends = json.loads(json.dumps(brazil_trends, indent=1))
      
      for trend in trends[0]["trends"]:
          print (trend["name"]).strip("#")
      

      更多解释可以在这里找到:Click here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-22
        • 2020-02-06
        • 2012-02-26
        • 1970-01-01
        • 2021-03-08
        • 2010-12-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多