首页 > 解决方案 > 尝试使用带有 Python 2.7 的 selenium 驱动程序从网页中提取列表

问题描述

我正在尝试使用 Selenium 驱动程序和 Python 提取存储在网页中的列表。

列表存储为

    ol class="My_Favorite_food"
        a href="/recipes/cuisine"
            li
                span style="margin-left: 10px;"South Indian 
                    span style="margin-left: 10px; margin-right: 5px; font-weight: 400;"Tomato Rice/span 
                    span style="font-size: 0.8em;"(TC)/span span style="float: right; margin-right: 10px;"60/span
                /span
            /li
        /a
    /ol

我尝试使用“find_elements_by_xpath”但收效甚微。我哪里出错了?我想提取“South Indian”、“Tomato Rice”、60

try:
    dropdown_list = br.find_elements_by_xpath('.//ol[@class="My_Favorite_food"]/a[@href="/recipes"]/li')
    items = dropdown_list.find_elements_by_tag_name("li")
    for item in items:
        Print ("Item text: >%s<>" % item.text)
except:
    print("That record is not found")

return 0 

标签: python-2.7selenium

解决方案


线

items = dropdown_list.find_elements_by_tag_name("li")

尝试在您的“li”标签中搜索“li”项目。将其更改为

items = dropdown_list.find_elements_by_tag_name("span")

应该做的伎俩。


推荐阅读