如何解决Tweepy search_full_archive() 缺少 2 个必需的位置参数:“label”和“query”
我正在使用 Tweepy 3.10.0 一次收集包含特定关键字和主题标签的推文。我最近从标准开发者帐户升级到高级帐户以访问完整档案。我知道这将“搜索”功能更改为“search_full_archive”并更改了其他一些小的语法内容。我以为我做了正确的更改,但我仍然收到此错误。我已经查看了 Developer API 参考。
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,wait_on_rate_limit=True)
def get_tweets_withHashTags(query,startdate,enddate,count = 300):
tweets_hlist= []
tweets_list= []
qt=str(query)
for page in tweepy.Cursor(api.search_full_archive,environment_name='FullArchive',q=qt,fromDate=startdate,toDate=enddate,count=300,tweet_mode='extended').pages(100):
count = len(page)
print( "Count of tweets in each page for " + str(qt) + " : " + str(count))
for value in page:
hashList = value._json["entities"]["hashtags"]
flag = 0
for tag in hashList:
if qt.lower() in tag["text"].lower():
flag = 1
if flag==1:
tweets_hlist.append(value._json)
tweets_list.append(value._json)
print("tweets_hash_"+ query +": " + str(len(tweets_hlist)))
print("tweets_"+ query +": " + str(len(tweets_list)))
with open("/Users/Victor/Documents/tweetCollection/data/"+startdate +"/" + "query1_hash_" + str(startdate)+ "_" + str(enddate) + "_" +query+'.json','w') as outfile:
json.dump(tweets_hlist,outfile,indent = 2)
with open("/Users/Victor/Documents/tweetCollection/data/"+startdate +"/"+"query1_Contains_" + str(startdate)+ "_" + str(enddate) + "_" +query+'.json','w') as outfile:
json.dump(tweets_list,indent = 2)
return len(tweets_list)
query = ["KeyWord1","KeyWord2","KeyWord3",etc.]
for value in query:
get_tweets_withHashTags(value,"2020-04-21","2020-04-22")
解决方法
根据 api 的代码 https://github.com/tweepy/tweepy/blob/5b2dd086c2c5a08c3bf7be54400adfd823d19ea1/tweepy/api.py#L1144 api.search_full_archive 将标签(环境名称)和查询作为参数。如此变化
api.search_full_archive,environment_name='FullArchive',q=qt,fromDate=startdate,toDate=enddate,count=300,tweet_mode='extended'
到
api.search_full_archive,label='FullArchive',query=qt,toDate=enddate
至于 tweet_mode='extended',它不适用于 search_full_archive 和 search_30_day。您可以在 https://github.com/tweepy/tweepy/issues/1461
中查看如何访问全文版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。