簡體   English   中英

如何使用 tweepy 搜索趨勢?

[英]How to search trends using tweepy?

我正在編寫一個機器人,我需要搜索我記錄的趨勢。 但是返回的搜索結果只是[],也就是什么都沒有。 這是代碼的粘貼箱。 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)

我如何解決它?

這些搜索都不會返回任何結果,因為沒有與這些查詢匹配的推文。
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("#")

更多解釋可以在這里找到: 點擊這里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM