【问题标题】:Struggling with a series of variables与一系列变量作斗争
【发布时间】:2014-04-06 18:22:20
【问题描述】:

为了避免推文被 twitter 垃圾邮件过滤器捕获,我有一些代码会转到 tinyurl 并在每次为每个原始 url 运行代码时创建一个新的短 url。我想要的是每次打印'u' 时,它的值应该传递给变量'linkvar1', 'linkvar2', 'linkvar3' 等。这是稍后在代码中传递给推文提交的:

import simplejson
import httplib2
import twitter
import tinyurl

print("Python will now attempt to submit tweets to twitter...")

try:

    api = twitter.Api(consumer_key='',
                      consumer_secret='',
                      access_token_key='',
                      access_token_secret='')

    for u in tinyurl.create('http://audiotechracy.blogspot.co.uk/2014/03/reviewing-synapse-antidote-rack.html',
                        'http://audiotechracy.blogspot.co.uk/2014/03/free-guitar-patches-for-propellerhead.html',
                        'http://audiotechracy.blogspot.co.uk/2014/02/get-free-propellerhead-rock-and-metal.html',
                        ):
        print u
        linkvar1 = u
        linkvar2 = u
        linkvar3 = u

    status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + linkvar1 + " #propellerhead #synapse")
status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + linkvar2 + " #propellerhead #reason #guitar")
status = api.PostUpdate("Free Metal and Rock drum samples!" + linkvar3 + " #propellerhead #reason)


print("Tweets submitted successfully!")

除了例外,e: 打印 str(e)
print("推特提交失败!!!")

然而,目前所有这些都是使用最后为所有提交的推文生成的 tinyurl。我确定这是一个简单的解决方法,但我只是觉得很愚蠢,但是有人知道如何做我想做的事吗?

谢谢

【问题讨论】:

    标签: python twitter tinyurl


    【解决方案1】:

    您的问题是您在每个循环中都没有对 linkvar 变量做任何事情。因此,每次循环运行时,它们都会被覆盖。

    你有几个选择

    选项 1:使 linkvars 成为您附加到每个循环的列表

    linkvar1 = []
    for u in ...
       ...
       linkvar1.append(u)
    
    # Post to twitter
    for p in linkvar1:
        status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + p + " #propellerhead #synapse")
        status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + p + " #propellerhead #reason #guitar")
        status = api.PostUpdate("Free Metal and Rock drum samples!" + p + " #propellerhead #reason)
    

    在第一个 for 循环结束时,您将在 linkvar 变量中获得值。我不确定你为什么要使用三个,我是否将它缩减为单个实例。然后,您可以使用另一个 for 循环进行循环,或者将整体销售传递给您自己的函数,该函数将适当地处理它们。无论哪种情况,您的所有网址现在都在每个变量的列表中

    选项 2:调用一个函数在每个循环上执行

    for u in ...
       ...
       MyTwitterFunction(u)
    
    def MyTwitterFunction(url):
        status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + url + " #propellerhead #synapse")
        status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + url + " #propellerhead #reason #guitar")
        status = api.PostUpdate("Free Metal and Rock drum samples!" + url + " #propellerhead #reason)
    

    每次循环迭代时,MyTwitterFunction 将使用 u 的值调用。

    选项 3:将您的发布代码直接拉入您的 for 循环

    for u in ...
        status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + u + " #propellerhead #synapse")
        status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + u + " #propellerhead #reason #guitar")
        status = api.PostUpdate("Free Metal and Rock drum samples!" + u + " #propellerhead #reason)
    

    这消除了对linkvar 变量和额外for 循环的需要。您可以直接从创建 URL 的循环中发布。

    【讨论】:

      【解决方案2】:

      我不确定您所说的“传递给变量”是什么意思。看起来好像您将 u 的值分配给 3 个变量中的每一个,然后覆盖它 - 例如:

       for x in range(5):
         y = x
      

      将导致值 4 分配给 y。你可能想列个清单吗?例如:

      y = []
      for x in range(5):
        y.append(x)
      

      这将导致

      y = [0,1,2,3,4]
      

      我认为这就是您使用 link1、2、3 变量的目标。

      【讨论】:

      • 基本上我创建了 21 个缩短的 URL,用于 21 条推文……每条推文都需要一个不同的链接。如果我以上述方式创建一个列表,我如何将每个列表的值依次传递给不同的推文?
      • @user3045351 稍后在您的代码中,您可以遍历列表并将它们的元素传递到您需要传递它们的任何地方。或者,正如 Andy 指出的那样(我希望我有),你也可以在你已经创建的主循环中调用你需要调用的任何函数。
      • 我对如何将您的答案应用于我的场景感到有些困惑,因此我修改了答案以显示我的完整代码,但简化为三个网址。
      • @ser3045351 - 缩进你的“status =”行,使它们落入for循环中。
      猜你喜欢
      • 2021-06-24
      • 2011-01-17
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 2015-09-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-08
      相关资源
      最近更新 更多