2017-04-18 79 views
3

我希望從這個環節中提取的歷史價格: https://pakstockexchange.com/stock2/index_new.php?section=research&page=show_price_table_new&symbol=KEL數據提取使用Python

要做到這一點,我用下面的代碼

import requests 
import pandas as pd 
import time as t 

t0=t.time() 

symbols =[ 
      'HMIM', 
      'CWSM','DSIL','RAVT','PIBTL','PICT','PNSC','ASL', 
      'DSL','ISL','CSAP','MUGHAL','DKL','ASTL','INIL'] 

for symbol in symbols: 
    header = { 
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", 
    "X-Requested-With": "XMLHttpRequest" 
} 
    r = requests.get('https://pakstockexchange.com/stock2/index_new.php?section=research&page=show_price_table_new&symbol={}'.format(str(symbol)), headers=header) 
    dfs = pd.read_html(r.text) 
    df=dfs[6] 
    df=df.ix[2: , ] 
    df.columns=['Date','Open','High','Low','Close','Volume'] 
    df.set_index('Date', inplace=True) 
    df.to_csv('/home/furqan/Desktop/python_data/{}.csv'.format(str(symbol)),columns=['Open','High','Low','Close','Volume'], 
      index_label=['Date']) 

    print(symbol) 


t1=t.time() 
print('exec time is ', t1-t0, 'seconds') 

上面代碼中提取從鏈路的數據將其轉換爲熊貓數據框並保存。

問題是,它需要很多時間,並且對於更多符號來說效率不高。任何人都可以建議任何其他方式以有效的方式實現上述結果。

此外,是否有任何其他編程語言可以在較短的時間內完成相同的工作。

+1

我會_guess_那個時候一個體面的部分是在阻塞GET請求。如果您嘗試異步運行請求,會發生什麼情況,例如與['request-futures'](https://github.com/ross/requests-futures)? – roganjosh

+0

不在我平時的電腦上,下載一些先決條件來測試:) – roganjosh

+0

我是編程新手,所以需要我花時間嘗試異步運行請求。閱讀文檔。 –

回答

2

具有requests的正常GET請求是「阻塞」的;一個請求被髮送,一個響應被接收並且然後被處理。至少有一部分處理時間用於等待響應 - 我們可以將所有請求與requests-futures異步發送,然後在準備好後收集響應。

這就是說,我認爲DSIL是超時或類似的東西(我需要看得更遠)。雖然我可以從symbols隨機選擇得到一個體面的加速,但兩種方法都需要約。同時如果DSIL在列表中。

編輯:似乎我撒謊,這只是一個不幸的巧合與「DSIL」多次。在symbols中您擁有的標籤越多,異步方法將越快超過標準requests

import requests 
from requests_futures.sessions import FuturesSession 
import time 

start_sync = time.time() 

symbols =['HMIM','CWSM','RAVT','ASTL','INIL'] 

header = { 
    "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.75 Safari/537.36", 
    "X-Requested-With": "XMLHttpRequest" 
} 

for symbol in symbols: 
    r = requests.get('https://pakstockexchange.com/stock2/index_new.php?section=research&page=show_price_table_new&symbol={}'.format(str(symbol)), headers=header) 

end_sync = time.time() 

start_async = time.time() 
# Setup 
session = FuturesSession(max_workers=10) 
pooled_requests = [] 

# Gather request URLs 
for symbol in symbols: 
    request= 'https://pakstockexchange.com/stock2/index_new.php?section=research&page=show_price_table_new&symbol={}'.format(symbol) 
    pooled_requests.append(request) 

# Fire the requests 
fire_requests = [session.get(url, headers=header) for url in pooled_requests] 
responses = [item.result() for item in fire_requests] 

end_async = time.time() 

print "Synchronous requests took: {}".format(end_sync - start_sync) 
print "Async requests took:  {}".format(end_async - start_async) 

在上面的代碼中,我獲得了3倍的加速獲取響應。您可以遍歷responses列表並正常處理每個響應。

編輯2: 通過異步請求的響應去,並將其保存爲您前面所做的:

for i, r in enumerate(responses): 
    dfs = pd.read_html(r.text) 
    df=dfs[6] 
    df=df.ix[2: , ] 
    df.columns=['Date','Open','High','Low','Close','Volume'] 
    df.set_index('Date', inplace=True) 
    df.to_csv('/home/furqan/Desktop/python_data/{}.csv'.format(symbols[i]),columns=['Open','High','Low','Close','Volume'], 
      index_label=['Date']) 
+0

幹得好。現在它快得多,但爲了保存數據幀中的響應,我無法使用異步方法來實現這一點。 –

+0

@FurqanHashim我已編輯re:DSIL標籤。應該沒有什麼能阻止你像往常一樣寫。讓我檢查並編輯。 – roganjosh

+0

@FurqanHashim請參閱編輯2 – roganjosh