如何使用 tweepy 搜索趋势?
How to search trends using tweepy?
我正在编写一个机器人,我需要搜索我记录的趋势。但是 returns 的搜索结果只是 [],也就是什么都没有。这是代码的 pastebin。 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)
我该如何解决?
None 这些搜索将 return 任何结果,因为没有与这些查询匹配的推文。
API.search
的第一个参数是搜索查询字符串,您搜索的是字符串本身,而不是它们引用的文件中的任何内容。
另外,您应该考虑使用循环。
试试这个:
# -*- 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
我正在编写一个机器人,我需要搜索我记录的趋势。但是 returns 的搜索结果只是 [],也就是什么都没有。这是代码的 pastebin。 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)
我该如何解决?
None 这些搜索将 return 任何结果,因为没有与这些查询匹配的推文。
API.search
的第一个参数是搜索查询字符串,您搜索的是字符串本身,而不是它们引用的文件中的任何内容。
另外,您应该考虑使用循环。
试试这个:
# -*- 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