阅读硒python

时间:2018-07-31 10:53:16

标签: python html django selenium

我如何阅读<dd>标签中具有<dt>之类的商品代码的文本。

`<dl class="dl">
        <dt>Trading Screen Product Name</dt>
        <dd>Biodiesel Futures (balmo)</dd>
        <dt>Trading Screen Hub Name</dt>
        <dd>Soybean Oil Pen 1st Line</dd>
        <dt>Commodity Code</dt>
        <dd><div>S25-S2Z</div></dd>
        <dt>Contract Size</dt>
        <dd><div>100 metric tonnes (220,462 pounds)</div></dd>
</dl>` 


from selenium import webdriver
driver = webdriver.Chrome("C:\\Python36-32\\selenium\\webdriver\\chromedriver.exe")
link_list = ["http://www.theice.com/products/31500922","http://www.theice.com/products/243"]
driver.maximize_window()
for link in link_list:
    driver.get(link)
    desc_list = driver.find_elements_by_class_name("dl")

2 个答案:

答案 0 :(得分:0)

尝试实现以下代码以获取"Commodity Code"的值作为输出:

for desc_list in driver.find_elements_by_class_name("dl"):
    print(desc_list.find_element_by_xpath("./dt[.='Commodity Code']/following-sibling::dd").text)

答案 1 :(得分:0)

要提取/读取带有子<dd>标签的<div>标签的文本,例如 S25-S2Z ,您可以创建所需元素的列表,然后在元素中打印文本,并可以使用以下解决方案:

for element in driver.find_elements_by_xpath("//dl[@class='dl']//dd/div"):
    list_dd.get_attribute("innerHTML")