2017-07-30 39 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())