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!")

except 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変数に値が含まれます。なぜ 3 つを使用しているのかわかりませんが、1 つのインスタンスだけに切り詰めましたか。次に、別のループを使用してループスルーするforか、ホールセールを適切に処理する独自の関数に渡します。いずれの場合も、すべての URL がこれらの変数のそれぞれのリストに含まれています。

オプション 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

forオプション 3: 投稿コードをループに直接プルする

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 に答える