python - 使用 Tweepy 发布带有 YouTube 视频 URL 的推文

标签 python python-3.x twitter youtube tweepy

我注意到,如果我(从浏览器)正常发推文并在消息后面加上 YouTube 视频链接,Twitter 会显示视频的缩略图,如下所示:

但是,如果我使用以下代码来发送推文:

import tweepy
import json
youtube_url = r'https://www.youtube.com/watch?v=tj-fmOnbBpU&t=0s'
# account tokens
twitter_keys = json.load(open('twitter_keys.json'))
auth = tweepy.OAuthHandler(twitter_keys["consumer_key"], twitter_keys["consumer_secret"]) # authentication of consumer key and secret
auth.set_access_token(twitter_keys["access_token"], twitter_keys["access_token_secret"]) # authentication of access token and secret
api = tweepy.API(auth)
twitter_text = "My message " + youtube_url 
api.update_status(status ="{}".format(twitter_text)) # send a tweet

我得到这样的东西:

正如您所看到的,即使在几天之后,Twitter 也不会显示该 URL 的预览。我不明白这是为什么。 如何修复我的代码,以便通过 API 发送的推文显示 YouTube 视频链接的预览?

最佳答案

As pointed out ,问题在于 URL 使用两个正斜杠作为路径,如屏幕截图所示。

关于python - 使用 Tweepy 发布带有 YouTube 视频 URL 的推文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60199259/

相关文章:

iphone - 网站上的 Twitter feed 无法在 iPad、iPhone 和类似设备上运行

node.js - 避免 Twitter API (node.js) 的多重验证

python - 如何将列字典转换为行字典?

python - Qt Designer 直接从设计器自动调整 tableWidget

python - 在Python中读取HTTP流

python-3.x - 在 Dataflow Python 中从 PubSub 读取 AVRO 消息

python - pyplot x轴被排序

python - 使用 NumPy 对灰度图像进行直方图均衡化

javascript - Bokeh:将变量传递给 Widgets 的 CustomJS

python - pyPdf 无法写入文件?