字典中每个条目的Python运行脚本

问题描述 投票:0回答:2

[我正在尝试编写一个简单的python程序,该程序将tweepy API用于twitter和wget,以从twitter帖子ID中检索图像链接(示例:twitter.com/ExampleUsername / 12345678),然后下载链接中的图片。实际的程序工作正常,但是有问题。尽管它为字典中的每个ID运行(如果有2个ID,它将运行2次),但它不会使用每个ID,因此脚本最终会查看字典中的最后一个ID,然后从中下载图像相同的ID,但是很多时候字典中都有一个ID。有谁知道如何使每个ID的脚本再次运行?

我希望程序查看第一个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
python dictionary twitter tweepy
2个回答
2
投票
我想通了!我知道该循环已用于某事...

0
投票
我看到的一件奇怪的事是,我在外循环中声明的变量在打开后再也不会使用。您的代码不应该是
© www.soinside.com 2019 - 2024. All rights reserved.