0

为了避免推文被 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!")

除了 Exception,e: print str(e)
print("Twitter 提交失败!!!")

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

谢谢

4

2 回答 2

2

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

你有几个选择

选项 1:制作linkvar附加到每个循环的 sa 列表

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 的循环中发布。

于 2014-04-06T18:34:49.673 回答
0

我不确定“传递给变量”是什么意思。看起来好像您正在将 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 变量的目标。

于 2014-04-06T18:30:08.367 回答