【发布时间】:2021-11-24 05:05:57
【问题描述】:
我正在通过 tweepy 使用 twitter api,但我不确定如何将未知数量的媒体 URL 保存到模型对象中。
媒体部分是我卡住的地方。有些推文会有媒体网址,有些没有,而有些可能有很多媒体网址。
如何创建未知数量的媒体 url 变量,然后如何将其添加到 update_or_create?
感谢您的所有帮助...我已经尝试解决这个问题一段时间了。
user = api.get_user(screen_name=team)
timeline = tweepy.Cursor(
api.user_timeline, id=team, exclude_replies=True,
include_rts=False).items(20)
handle = user['screen_name']
location = user['location']
description = user['description']
for tweet in timeline:
tweet_id = tweet['id']
date = tweet['created_at']
content = tweet['text']
`This is where I am stuck`
if 'media' in tweet.entities:
for media in tweet.extended_entities.media:
url = media['media_url']
Tweets.objects.update_or_create(
tweet_id=tweet_id, defaults={
'handle': handle,
'tweet_id': tweet_id,
'location': location,
'description': description,
'date': date,
'content': content,
'url': url}
)
【问题讨论】: