2017-09-25 109 views
1

我正在製作一個應用程序,用於搜索google關鍵詞並將搜索到的URL附加到列表中。然後將網址發佈到twitter。如何隨機選取列表中的下一個元素?

我該如何在每小時隨機時間從列表中選取一個URL,然後在隨後的一個小時內按順序在列表中選擇下一個URL?

import tweepy, time 
from google import search 

CONSUMER_KEY = 'xxxx' 
CONSUMER_SECRET = 'xxxx' 

# Create a new Access Token 
ACCESS_TOKEN = 'xxxx' 
ACCESS_SECRET = 'xxxx' 


auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) 
auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) 
api = tweepy.API(auth) 

#search google for url key links 

lst = [] #list holding URL 
for url in search('python', stop=20): 
    lst.append(url) 
    print(url) 

lst = [ x for x in lst if "wiki" not in x ] 

# What the bot will tweet 
for line in lst: 
    api.update_status(line) 
    print line 
    print '...' 
    time.sleep(1) 

print "All done!" 

在此先感謝您。

+1

到底在堅持什麼? – glibdud

+0

@glibdud我被困住了,如何從每小時/每小時隨機時間的列表中選取一個元素 – RustyShackleford

+1

儘管如此,還是有很多步驟。你需要知道如何生成['random'](https://docs.python.org/3.6/library/random.html)數字嗎?你只是在尋找['sched'](https://docs.python.org/3.6/library/sched.html)模塊? – glibdud

回答

0

這行代碼似乎工作,張貼隨機每隔一小時時:

for line in lst: 
    #api.update_status(line) 
    print line 
    print '...' 
    sleep(random.randint(1,60)) 
相關問題