2014-04-06 3 views
0

다음 코드는 거의 올바르게 작동합니다. 3 개의 URL을 짧게하고 3 개의 다른 트윗 내용을 입력 한 다음 트위터에 제출합니다. 그러나 URL이 짧아 질 때마다 단축 URL은 동일합니다. 이 때문에 트윗은 트위터 스팸 필터에 의해 계속 잡히고 있습니다.단축 URL은 임의로 지정해야합니다.

단축 URL의 모양을 무작위로 선택하여 가져 오기 tinyurl 또는 일부 다른 방법을 사용하여이를 방지 할 수 있습니까?

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 submissions have failed!!!") 

감사합니다 당신이 tinyurl.create의 결과를 통해 루프, 당신은 세 가지 linkvar 변수마다 그것을 할당하고

+0

가능한 중복 (http://stackoverflow.com/questions/22898046/struggling-with-a-series-of-variables) –

답변

0

, 그래서 루프를 종료 할 때 모든 세의 마지막 값과 동일합니다 u. 당신은 항상 동일한 URL 수를 처리하기 위하여려고하는 경우에

, 당신은 단지 변수에 명시 적으로 할당 할 수

linkvar1, linkvar2, linkvar3 = 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', 
) 

URL의 수가 변경 될 경우에, 당신은을 사용하는 것이 더 낫다 list하고 결과를 색인 당신이 원하는 :

linkvars = 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', 
) 
status = api.PostUpdate("The new Synapse Antidote Rack Extension:" + linkvars[0] + " #propellerhead #synapse") 
status = api.PostUpdate("Free Propellerhead guitar patches for everyone!" + linkvars[1] + " #propellerhead #reason #guitar") 
... 
[변수의 일련의 고투]의
+0

죄송합니다. 사이트에서 어떤 이유로 든 게시물을 편집 할 수있게되었습니다. 일단 들여 쓰기가 필요하다는 것을 알게되면 기본적으로 for 루프 내에서 의도해야합니다. 그런 식으로 u의 첫 번째 인스턴스는 첫 번째 짹짹, 두 번째 짹짹 두 번째 및 세 번째 짹짹 세번째 먹이를 가져옵니다. 문제는 tinyurl에 의해 생성 된 URL이 항상 동일하다는 것입니다. 단축 링크가 항상 동일하기 때문입니다. 이 문제를 해결할 수있는 사이트 자체에 사용자 지정 도메인 옵션이 있지만 코드 작성 방법에 대한 예제 구문을 찾을 수 없습니다. – gdogg371

+0

입력하신 코드가 올바르지 않으면 업데이트하십시오. 마치 세 개의 단축 URL, 즉 모든 3 개의 linkvars에 할당 한 첫 번째 URL, 3 개의 모든 linkvars에 할당 한 두 번째 URL 등을 반복합니다. 루프가 완료되면 모든 3 개의 linkvars가 _last_ 단축 URL과 같습니다. 위에서 제공 한 첫 번째 제안을 시도해 보셨습니까? 내가 그것을 실행할 때, 변수에 3 개의 다른 tinyurl이 할당됩니다. –

+0

@matthewtrevor ... 위와 같이 위선적 인 이유는 ... 더 이상 코드를 편집 할 수 없기 때문입니다. 브라우저 문제인지 사이트 문제인지는 잘 모릅니다. – gdogg371