如何使用 tweepy 搜索趋势?

2024-10-03 23:29:30 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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)

我该怎么修


Tags: txtapijsonlangsearchastypewith
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

这些搜索都不会返回任何结果,因为没有与这些查询匹配的tweet。
^{}的第一个参数是搜索查询字符串,您搜索的是字符串本身,而不是它们引用的文件中的任何内容。
此外,还应该研究如何使用循环

相关问题 更多 >