首页 > 解决方案 > 使用 ID 列表仅抓取不包含推文文本的推文元数据

问题描述

上下文:我有一个推文 ID 列表及其文本内容,我需要抓取它们的元数据。但是,我的代码也会抓取推文元数据和文本。由于我有大约 100K 的推文 ID,我不想浪费时间再次抓取推文文本。

问题:如何调整以下代码,以便仅下载推文元数据。我正在使用 tweepy 和 python 3.6。

def get_tweets_single(twapi, idfilepath):
    #tweet_id = '522778758168580098'
    tw_list = []
    with open(idfilepath,'r') as f1:#A File that Contains tweet IDS
        lines = f1.readlines()
        for line in lines:
            try:
                print(line.rstrip('\n'))
                tweet = twapi.get_status(line.rstrip('\n'))#tweepy function to crawl tweet metadata
                tw_list.append(tweet)
                #tweet = twapi.statuses_lookup(id_=tweet_id,include_entities=True, trim_user=True)
                with open(idjsonFile,'a',encoding='utf-8')as f2:
                    json.dump(tweet._json,f2)
            except tweepy.TweepError as te:
                print('Failed to get tweet ID %s: %s', tweet_id, te.message)

def main(args):
    print('hello')
# connect to twitter
    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
    auth.set_access_token(OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    api = tweepy.API(auth)
    get_tweets_single(api, idfilepath)

标签: python-3.xtwittermetadatatweepy

解决方案


您不能下载有关推文的元数据。

查看文档,您可以选择排除有关用户的信息trim_user=true- 但这是您唯一可以删除的内容。


推荐阅读