2017-10-12 192 views
1

我有一個訪問網站超過100次的腳本,並且希望在每個項目之間添加一個小的延遲。此外,如果可能的話,可能會添加一個倒計時或進程欄來顯示剩下的項目的狀態或數量。代碼示例如下;在for-in循環中添加暫停

import pandas as pd 

urls = ['https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/440/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/441/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/442/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/443/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/445/vehicletype/car?format=csv', 
    'https://vpic.nhtsa.dot.gov/api/vehicles/GetModelsForMakeIdYear/makeId/448/vehicletype/car?format=csv']    


dfs = [pd.read_csv(url) for url in urls] 
df = pd.concat(dfs, ignore_index=True)                      
df.to_csv('foo.csv')                           
+0

閱讀有關'tqdm'一個倒計時欄和'time.sleep'爲暫停執行。 – user1190882

+0

好奇 - 爲什麼你想添加一個暫停? – Parfait

+0

@Parfait因爲運行腳本在如此多的服務器請求後如此快地訪問相同的服務器,服務器會阻止它。 –

回答

3
import time 
dfs = [] 
num = len(urls) 
for i, url in enumerate(urls): 
    dfs.append(pd.read_csv(url)) 
    print 'Process {}, {} left'.format(i+1, num-i-1) 
    time.sleep(2) 
+0

太棒了!完美的作品! –

+0

@KyleRichards :) – galaxyan