使用 Tweepy 将推文的媒体、扩展链接、引号附加到推文文本
Append tweet's media, expanded links, quotes to the tweet text using Tweepy
例如,使用我当前的代码,this tweet 显示为:
Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https:// t.co/W0uFKU9jCr
我想要看起来像 Twitter 网站上显示的那样,例如:
Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https://www.instagram.com/p/BSocl5Djf5v/
我该怎么做呢?我的意思是将 Twitter 的短网址替换为媒体网址、扩展网址、推文引号……我知道这与 json 中的 'entities' object 有关,但我不确定如何处理在我的代码中
for status in new_tweets:
if ('RT @' not in status.full_text):
id = status.id
text = status.full_text
你说得对,你需要使用实体。你可以这样得到 expanded_url:
for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
if status.entities['urls']:
for url in status.entities['urls']:
links = url['expanded_url']
print(links)
您可以将状态文本和 expanded_url 串联起来打印出来
for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
if status.entities['urls']:
for url in status.entities['urls']:
links = url['expanded_url']
print(status.text + links)
并不是说这段代码只会在推文有 URL 时打印,所以我相信如果没有媒体 link 共享,它不会打印推文。
例如,使用我当前的代码,this tweet 显示为:
Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https:// t.co/W0uFKU9jCr
我想要看起来像 Twitter 网站上显示的那样,例如:
Stunning ride through the Oxfordshire lanes this morning to get the legs ready for the Fast Test… https://www.instagram.com/p/BSocl5Djf5v/
我该怎么做呢?我的意思是将 Twitter 的短网址替换为媒体网址、扩展网址、推文引号……我知道这与 json 中的 'entities' object 有关,但我不确定如何处理在我的代码中
for status in new_tweets:
if ('RT @' not in status.full_text):
id = status.id
text = status.full_text
你说得对,你需要使用实体。你可以这样得到 expanded_url:
for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
if status.entities['urls']:
for url in status.entities['urls']:
links = url['expanded_url']
print(links)
您可以将状态文本和 expanded_url 串联起来打印出来
for status in tweepy.Cursor(twitter_api.user_timeline, screenname=username).items(limit):
if status.entities['urls']:
for url in status.entities['urls']:
links = url['expanded_url']
print(status.text + links)
并不是说这段代码只会在推文有 URL 时打印,所以我相信如果没有媒体 link 共享,它不会打印推文。