如何使用Selenium和Python遍历项目列表并提取特定部分

2024-09-24 00:28:15 发布

您现在位置:Python中文网/ 问答频道 /正文

enter image description here在这个网页https://meshb.nlm.nih.gov/treeView中,我想遍历树的每个节点,如果我在它们的项目中看到单词“carvodiary…”,我想创建一个字典,列出顶级节点以及所有心血管相关项目。例如,在上面的页面中,您可以看到如果您展开“解剖学[A]”,您将看到心血管。现在,我想要这一部分,以及任何包含在心血管系统中的东西,如果你扩展它的话。我希望遍历html页的某些元素的一部分如下所示:

<a class="ng-scope">
   <span class="ng-binding ng-scope">Anatomy [A]</span>
</a>
    <ul class="treeItem ng-scope">
        <li class ="ng-scope" >
              < a  class ="ng-scope" href="/record/ui?ui=D001829" >
              < span  class ="ng-binding ng-scope" > Body Regions[A01] < / span >
              </a>
        </li>
        < li class ="ng-scope" >
              <a  class ="ng-scope" href="/record/ui?ui=D001829" >
                < span  class ="ng-binding ng-scope" > Cardio Vascular< / span >
              </a>
                    <ul class="treeItem ng-scope">
                        <li class="ng-scope">
                           <a class="ng-scope" href="/record/ui?ui=D015824">
                           <span class="ng-binding ng-scope">Blood-Air Barrier [A07.025]</span>
                           </a>
                                 <ul class="treeItem ng-scope">                    
                                   <li class="ng-scope">
                                       <a class="ng-scope" href="/record/ui?ui=D018916">
                                       <span class="ng-binding ng-scope">Blood-Aqueous Barrier [A07.030]</span>                        
                                       </a>
                                    </li>
                                 </ul>
                        </li>
                    </ul>
        </li>
    </ul>

。。。。。 这就是我目前所能做到的!在Python中,作为第一步,我想遍历顶级节点并找到单词“carvenous..”,但我一直看到错误“no such element:Unable to locate element”。有人能告诉我我错过了什么吗?你知道吗

from selenium import webdriver
chrome_path=r"G:\My Drive\A\chrome_driver\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get('https://meshb.nlm.nih.gov/treeView')
for links in driver.find_elements_by_css_selector('a.ng-scope'):
    cardio = links.find_element_by_css_selector('li>a>span.ng-binding.ng-scope')        
    print(cardio.text)

Tags: ui节点driverlielementrecordngul
1条回答
网友
1楼 · 发布于 2024-09-24 00:28:15

你的代码中有一些问题。除非单击父节点上的“+”图标,否则不能遍历列表。你知道吗

在您的代码中,我可以看到您已经创建了一个包含父节点(如解剖学、有机体等)的列表,但是您还没有编写扩展该列表的代码。你知道吗

您必须遵循以下步骤:

  1. 在列表中存储父节点=>;此步骤包含在代码中。你知道吗
  2. 通过单击展开图标(+icon)=>;遍历每个父节点。你知道吗
  3. 将子节点存储在列表中并遍历子节点=>;需要覆盖
  4. 继续迭代,除非您发现需要覆盖子节点“carvodiary”=>。你知道吗
  5. 单击子节点“carvious”前面的+图标,并将节点“carvious”下的元素存储在dictionary=>;needs to be covered中。你知道吗

我已经为您创建了一个代码,包括第一、第二和第三步。请以同样的方式进行。你知道吗

from selenium import webdriver
chrome_path=r"G:\MyDrive\A\chrome_driver\chromedriver_win32\chromedriver.exe"
driver=webdriver.Chrome(chrome_path)
driver.get('https://meshb.nlm.nih.gov/treeView')
for links in driver.find_elements_by_css_selector('a.ng-scope'):
    links.find_element_by_xpath("./following-sibling::span/i[1]").click();
      for sublinks in links.find_elements_by_xpath('./following-sibling::ul/li//a'):
        print(sublinks.text)

我有java的背景,所以请原谅我的任何语言相关的语法问题。你知道吗

相关问题 更多 >