2017-01-03 45 views
2

我需要使用python從本網站的表中獲取數據https://www.cashbackforex.com/en-US/tools/economic-impacts.aspx。 我寫的代碼到目前爲止是使用多個輸入在python中刮寫網頁

from bs4 import BeautifulSoup 
import requests 

url = 'https://www.cashbackforex.com/en-US/tools/economic-impacts.aspx' 

with requests.Session() as session: 
    session.headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36'} 

    # parsing parameters 
    response = session.get(url) 
    soup = BeautifulSoup(response.content, "lxml") 
    print(soup.select('input[type="button"]')) 
    data = { 
     'dnn$ctr1601$Chart$ddlCurrencies': 'USD', 
     'dnn$ctr1601$Chart$ddlReports': 'US Change in NonFarm Payrolls', 
     'dnn$ctr1601$Chart$ddlTimeZone': '(UTC) Coordinated Universal Time', 
     '__EVENTTARGET': soup.find('input', {'name': '__EVENTTARGET'}).get('value', ''), 
     '__EVENTARGUMENT': soup.find('input', {'name': '__EVENTARGUMENT'}).get('value', ''), 
     '__VIEWSTATE': soup.find('input', {'name': '__VIEWSTATE'}).get('value', ''), 
     '__VIEWSTATEGENERATOR': soup.find('input', {'name': '__VIEWSTATEGENERATOR'}).get('value', ''), 
     'btnApplyTools': soup.find('input', {'id': 'btnApplyTools'}).get('value', '') 
    } 

    # parsing data 
    response = session.post(url, data=data) 

    soup = BeautifulSoup(response.content, "lxml") 
    print(soup) 

但我每次運行程序時,我不能在表中找到值。我認爲該程序不會將輸入值發送到服務器,但我不確定。

下表:

enter image description here

+0

什麼要求你試圖模仿?當我點擊紅色的「應用」按鈕時,我在瀏覽器開發工具中看不到'economic-impact.aspx'的POST。 – alecxe

+0

當我更改一個變量,即美元,美國非農工資的變化並點擊應用底部。下表中稱爲「事件歷史」,更改。 我需要提取該表。但是當我用BeautifulSoup讀這張表時,那些行不存在。 –

回答

2

我檢查提供的網頁,發現存在Session()沒有必要和發送多個參數,以獲得所需的表。所有你需要的是指定inst參數(如過濾器的標識符)和timezone。例如,對於USD/US Change in NonFarm Payrollsinst參數值是10332295,timezone對於(UTC) Coordinated Universal Time3

所以你的要求應該是這樣

params = {'inst': '10332295', 'timezone': '3'} 
response = requests.get('https://www.cashbackforex.com/DesktopModules/Chart/HistoricalEventFigures.ashx', params=params) 

然後你就可以在方便的方式解析response,如:

from xml.dom import minidom 

xml = minidom.parseString(response.text) 
print([i.childNodes[0].wholeText for i in xml.getElementsByTagName("Date")]) 
print([i.childNodes[0].wholeText for i in xml.getElementsByTagName("ReportName")]) 
... 

輸出:

['2 Dec 2016', '4 Nov 2016', '7 Oct 2016', '2 Sep 2016', '5 Aug 2016', '8 Jul 2016', '3 Jun 2016',...] 
['US Change in NonFarm Payrolls', 'US Change in NonFarm Payrolls', 'US Change in NonFarm Payrolls', 'US Change in NonFarm Payrolls',...] 
+0

感謝您的回答。你的代碼部分工作,但我需要該程序通過所有的下拉列表。所以我可以得到他們所有人。例如,它需要從澳元開始查找從AU銀行假日開始的所有事件 - ANZAC日到最後。 你是怎麼找到「https://www.cashbackforex.com/DesktopModules/Chart/HistoricalEventFigures.ashx」的? –

+0

當您點擊Apply按鈕時,您會發送3個GET請求:https://www.cashbackforex.com/Dark/ DesktopModules/Chart/Candles.ashx,用於顯示圖表上顯示的數據:https://www.cashbackforex .com/DesktopModules/Chart/SameTimeReports.ashx「作爲第一個表格,而」https:// www.cashbackforex.com/DesktopModules/Chart/HistoricalEventFigures.ashx「作爲第二個(」事件歷史「)表格。 – Andersson

+0

每一對'AUD'-'AU Bank Holiday - ANZAC'這樣的'Event finder'都有它自己的'inst'參數,但我不知道你應該申請'AUD'對的'inst'值的確切範圍因爲它們似乎不是連續的... – Andersson