2017-07-30 98 views
1

在每一個星期一,我需要解析上週(5天)「美元匯率1100」。所以,爲此我需要選擇上週每天的日期和系列下的「1100美元」。我只需要1年的男高音率。所以最終的答案應該有5年的1年期「美元匯率1100」。python中的URL解析(webparsing)

https://www.theice.com/marketdata/reports/180

from selenium import webdriver 
chrome_path = r"C:\Users\vick\Desktop\python_1\chromedriver.exe" 
driver = webdriver.Chrome(chrome_path) 
driver.get("https://www.theice.com/marketdata/reports/180") 
driver.find_element_by_xpath() 
+0

你能告訴我們任何代碼,你沒試過嗎? –

+0

要獲得xpath的鉻,所有你需要做的是檢查,右鍵單擊元素,選擇複製,然後通過xpath –

+0

我已經返回代碼(添加問題)這是非常基本的。我沒有得到xpath中的任何東西,我可以通過它解析所有必需的值 – ads

回答

0
from selenium import webdriver 
chrome_path = r"C:\Users\vick\Desktop\python_1\chromedriver.exe" 
driver = webdriver.Chrome(chrome_path) 
driver.get("https://www.theice.com/marketdata/reports/180") 
try: 
    driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/div[2]/button').click() 
except: 
    pass 

driver.find_element_by_xpath('//*[@id="seriesNameAndRunCode_chosen"]/a/span').click() 
driver.find_element_by_xpath('//*[@id="seriesNameAndRunCode_chosen"]/div/ul/li[5]').click() 
driver.find_element_by_xpath('//*[@id="reportDate"]').clear() 
driver.find_element_by_xpath('//*[@id="reportDate"]').send_keys("27-Jul-2017") #or whatever date 
driver.find_element_by_xpath('//*[@id="selectForm"]/input').click() 
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)/2;") 
print (driver.find_element_by_xpath('//*[@id="report-content"]/div/div/table/tbody/tr[1]/td[2]').get_attribute('innerHTML')) 

這將選擇USD RATES 1100,輸入日期,並提交。您可以循環此過程以輸入更多日期。

+0

以下看到我的回答剛剛編輯它爲接受按鈕 –

+0

先生,非常感謝您的答案。但對我來說,而不是「美元RATS 1100」,它仍然給「110歐元」 – ads

+0

發現bug,將修復 –

0

最終的答案:

from selenium import webdriver 
chrome_path = r"C:\Users\vick\Desktop\python_1\chromedriver.exe" 
driver = webdriver.Chrome(chrome_path) 
driver.get("https://www.theice.com/marketdata/reports/180") 
try: 
    driver.find_element_by_xpath('/html/body/div[3]/div/div[2]/div/div/ 
    div[2]/button').click() 
except: 
     pass 

driver.find_element_by_xpath('//* 
[@id="seriesNameAndRunCode_chosen"]/a/span').click() 
driver.find_element_by_xpath('//* 
[@id="seriesNameAndRunCode_chosen"]/div/ul/li[5]').click() 
driver.find_element_by_xpath('//*[@id="reportDate"]').clear() 
driver.find_element_by_xpath('//*[@id="reportDate"]').send_keys("27-Jul- 
2017") #or whatever date 
driver.find_element_by_xpath('//*[@id="selectForm"]/input').click() 
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)/2;") 
time.sleep(4) 
print ((driver.find_element_by_xpath('//*[@id="report- 
content"]/div/div/table/tbody/tr[1]/td[2]').get_attribute('innerHTML')). 
strip())