为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

明月笑刀无情 2022-10-11 16:02:51
我正在尝试编写一个简单的 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-8import tweepy #https://github.com/tweepy/tweepyimport wget#Twitter API credentialsconsumer_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
查看完整描述

2 回答

?
至尊宝的传说

TA贡献1789条经验 获得超10个赞

我想到了!我知道循环被用于某些事情......


我将所有内容从a = len(tweetstowget.download(url)移到for tweet in tweets:循环中,并删除了for i in id_list:循环。


感谢 tdelany,这个程序现在可以运行了!感谢大家!


如果有人想要,这是新代码:


#!/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 = []

        tweets.extend(api.statuses_lookup(id_=id_list, include_entities=True))

        for tweet in tweets:

            a = len(tweets)

            print(tweet.entities['media'][0]['media_url'])

            url = tweet.entities['media'][0]['media_url']

            wget.download(url)

get_all_tweets()


查看完整回答
反对 回复 2022-10-11
?
慕桂英4014372

TA贡献1871条经验 获得超13个赞

我看到的一件奇怪的事情是,我在外循环中声明的变量在 on 之后从未使用过。你的代码不应该是

 tweets.extend(api.statuses_lookup(id_=i, include_entities=True))

而不是你写的 id_=id_list ?


查看完整回答
反对 回复 2022-10-11
  • 2 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信