我正在尝试编写一个简单的 python 程序,它使用 tweepy API for twitter 和 wget 从 twitter 帖子 ID(例如:twitter.com/ExampleUsername/12345678)中检索图像链接,然后从链接下载图像。实际程序运行良好,但有一个问题。虽然它针对字典中的每个 ID 运行(如果有 2 个 ID,它会运行 2 次),但它不会使用每个 ID,因此脚本最终会查看字典中的最后一个 ID,然后从中下载图像相同的 id 但是很多时候字典中有一个 ID。有谁知道如何让每个 ID 的脚本再次运行?
tl; dr 我希望程序查看第一个 ID,获取其图像链接,下载它,然后对下一个 ID 执行相同的操作,直到完成所有 ID。
#!/usr/bin/env python
# encoding: utf-8
import tweepy #https://github.com/tweepy/tweepy
import wget
#Twitter API credentials
consumer_key = "nice try :)"
consumer_secret = "nice try :)"
access_key = "nice try :)"
access_secret = "my, this joke is getting really redundant"
def get_all_tweets():
#authorize twitter, initialize tweepy
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
id_list = [1234567890, 0987654321]
# Hey StackOverflow, these are example ID's. They won't work as they're not real twitter ID's, so if you're gonna run this yourself, you'll want to find some twitter IDs on your own
# tweets = api.statuses_lookup(id_list)
for i in id_list:
tweets = []
tweets.extend(api.statuses_lookup(id_=id_list, include_entities=True))
for tweet in tweets:
spacefiller = (1+1)
# this is here so the loop runs, if it doesn't the app breaks
a = len(tweets)
print(tweet.entities['media'][0]['media_url'])
url = tweet.entities['media'][0]['media_url']
wget.download(url)
get_all_tweets()
谢谢,~CS
至尊宝的传说
慕桂英4014372
相关分类