使用 tweepy 获取推文 url 或推文 ID [英] Get tweet url or tweet id using tweepy
问题描述
因此,正如问题所暗示的那样,我正试图弄清楚如何获取推文 ID # 或整个网址.使用下面的代码,我可以获得我想要的推文并将它们打印出来.但是,我不想打印它们,而是想获取 url 或至少是推文 id(以便我可以制作自己的 url),以便稍后在我的代码中使用.有什么建议吗?在 tweepy 文档中没有看到关于此的任何内容,但也许我缺少某些东西或我可以使用其他东西来实现这一目标?
So as the question suggests I'm trying to figure out how to get either the tweet id # or the whole url. Using the code below I can get the tweet(s) i want and print them out. However instead of printing them I want to get either the url or at least the tweet id (so that I can make my own url) that I can use in my code later down the line. Any advice? not seeing anything in the tweepy docs about this but perhaps there is something i'm missing or something else I can use to achieve this?
import tweepy
# Authenticate to Twitter
auth = tweepy.OAuthHandler("*******", "*****")
auth.set_access_token("******", "*****")
api = tweepy.API(auth)
# Function to extract tweets
def get_tweets(username):
tweets = api.user_timeline(screen_name=username,count = 1)
tmp=[]
tweets_for_csv = [tweet.text for tweet in tweets]
for t in tweets_for_csv:
tmp.append(t)
print(tmp)
if __name__ == '__main__':
get_tweets("@username")
这段代码工作正常,只是试图了解我将添加什么以获得所需的数据.
This code works fine just trying to learn what I would add to get the desired data.
推荐答案
如果你有推文 ID,你可以像这样到达最终 URL:
If you have the Tweet ID, you can get to the final URL like this:
将其附加到 https://twitter.com/twitter/statuses/
(+ id)
Append it to https://twitter.com/twitter/statuses/
(+ the id)
遵循 HTTP 重定向.
Follow the HTTP redirect.
听起来您似乎无法获得 ID 本身 - 这将在每个推文的 tweet.id
值中 - 目前您只是在保存 tweet.text代码>
It sounds like you're not able to get the ID itself - that will be in the tweet.id
value for each Tweet - currently you're just saving tweet.text
这篇关于使用 tweepy 获取推文 url 或推文 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!