【问题标题】:url parsing(webparsing) in pythonpython中的url解析(webparsing)
【发布时间】:2017-07-30 17:24:16
【问题描述】:

每个星期一,我需要解析上周(5 天)“USD RATES 1100”。因此,为此我需要为上周的每一天选择日期,并从系列下拉列表中选择“USD RATES 1100”。我只需要 1 年的期限利率。所以最终答案应该有 5 个 1 年期限的“USD RATES 1100”值。

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()

【问题讨论】:

  • 您能告诉我们您尝试过的任何代码吗?
  • 要在chrome中获取xpath,您所要做的就是检查,右键单击元素,选择复制,然后通过xpath
  • 我已经返回了非常基本的代码(有问题添加)。我在 xpath 中没有得到任何可以解析所有必需值的东西
  • //*[@id="seriesNameAndRunCode_chosen"]/a/span
  • 试试这个 xpath。然后点击

标签: python parsing url-parsing


【解决方案1】:
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,输入日期并提交。您可以循环此过程以输入更多日期。

【讨论】:

  • 先生,非常感谢您的回答。但对我来说,它仍然给出“EUR RATES 110”而不是“USD RATS 1100”
  • 发现bug,会修复
  • 先生,非常感谢。我发现这很难解决。我仍然有疑问,打开页面后,当我在检查元素时,我仍然无法找到与“1 年”相对应的“usd rate 1100”值。检查元素显示相同的原始页面。再次非常感谢您回答我的问题。
  • 一年号的XPATH是//*[@id="report-content"]/div/div/table/tbody/tr[1]/td[2]
  • 它给了我这个错误:NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="report- content"]/div/div/table/tbody/tr[1]/td[2]"} (会话信息: chrome=59.0.3071.115) (驱动程序信息: chromedriver=2.31.488763 (092de99f48a300323ecf8c2a4e2e7cab51de5ba8),platform=Windows NT 10.0.15063 x86_64)
【解决方案2】:

最终答案:

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() )

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2015-06-09
    • 1970-01-01
    • 2016-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多