python-3.x - 使用 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)
解决方案
您不能只下载有关推文的元数据。
查看文档,您可以选择排除有关用户的信息trim_user=true
- 但这是您唯一可以删除的内容。
推荐阅读
- javascript - 续集调用结构
- javascript - 在节点 js 中使用 promise 时函数工作不正确
- javascript - VueJS router-link ctrl+click 带有标签元素的链接
- javascript - 获取文本“
- " 带有 javascript 的标签
- javascript - 如何从.map返回对象数组
- javascript - 元掩码 web3 未定义
- javascript - 创建没有封装Javascript的库
- bash - bash 在继续之前等待打开的应用程序完成
- javascript - react-select 可以加载异步数据
- javascript - Material-UI SelectField onChange 不起作用