python - Selenium webdriver 链接提取

标签 python selenium selenium-webdriver

我的 HTML 源代码为

   <ul class="content">
    <li class="">
     <div class="profile-card">
      <div class="content">
       <a href="https://www.linkedin.com/in/ouafae-ezzine-894b113">
         Ouafae Ezzine
        </a>
       <p class="headline">
        Organise vos evenements professionnels &amp; personnels
       </p>
       <dl class="basic">
        <dt>
         Location
        </dt>
        <dd>
         France
        </dd>
        <dt>
         Industry
        </dt>
       </dl>
       <table class="expanded hide-mobile">
        <tbody>
         <tr>
          <th>
           Current
          </th>
          <td>
           Responsable at Blue Med Events
          </td>
         </tr>
         <tr>
          <th>
           Past
          </th>
          <td>
           Administrateur achats at Pfizer
          </td>
         </tr>
         <tr>
          <th>
           Education
          </th>
          <td>
           Universite d'Evry Val d'Essonne
          </td>
         </tr>
         <tr>
          <th>
           Summary
          </th>
          <td>
           Riche d'une experience de plus de 25 ans dans le domaine de l'organisation evenementielle, je mets mon expertise...
          </td>
         </tr>
        </tbody>
       </table>
      </div>
     </div>
    </li>
    <li class="">
     <div class="profile-card">
      <div class="content">
       <h3>
        <a href="https://www.linkedin.com/in/ouafae-ezzine-892855b6">
         Ouafae Ezzine
        </a>
       </h3>
       <p class="headline">
        Gerante
       </p>
       <dl class="basic">
        <dt>
         Location
        </dt>
        <dd>
         France
        </dd>
        <dt>
         Industry
        </dt>
        <dd>
         Events Services
        </dd>
       </dl>
       <table class="expanded hide-mobile">
        <tbody>
         <tr>
          <th>
           Current
          </th>
          <td>
           Gerante
          </td>
         </tr>
        </tbody>
       </table>
      </div>
     </div>
    </li>
   </ul>

我编写了一个 python 代码,它将查找页面中是否存在给定的字符串。

如果字符串与该配置文件( anchor 标记)关联,我正在尝试编写逻辑来提取与特定配置文件关联的 anchor 链接。

我的Python片段:

from selenium import webdriver

driver = webdriver.Firefox()
driver.get('file:///nfs/users/lpediredla/Documents/linkedin/Top2profLinkedIn.html')

ids = driver.find_elements_by_xpath("//*[contains(text(), 'Organise vos evenements professionnels')]")

#don't know how to associate the element with the profile
#please help with the logic here.


driver.close()

此时,我在尝试将元素与其所在的配置文件存储桶关联起来时感到惊讶。

非常感谢任何帮助。

最佳答案

你想要的是preceding-sibling::a查找包含文本p标记之前的anchor标记'Organise vos Evenements professionalnels':

"//p[contains(text(), 'Organise vos evenements professionnels')]/preceding-sibling::a"

使用您的 html:

In [11]: from lxml.html import fromstring

In [12]: xml = fromstring(html)

In [13]: print(xml.xpath("//p[contains(text(), 'Organise vos evenements professionnels')]/preceding-sibling::a"))
[<Element a at 0x7f5cae670188>]

In [14]: print(xml.xpath("//p[contains(text(), 'Organise vos evenements professionnels')]/preceding-sibling::a//text()"))
['\n         Ouafae Ezzine\n        ']

如果您想要不区分大小写的匹配,您可以 translate :

 "//p[contains(translate(text(),'ORGANISEVOSPRLT','organisevosprlt'), 'organise vos evenements professionnels')]/preceding-sibling::a"

关于python - Selenium webdriver 链接提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522607/

相关文章:

python - 使用np.where使用自创建函数向pandas添加列

python - 去除 unicode 字符修饰符

selenium - Chrome 网络驱动程序 : Curl error thrown for http DELETE to/session/XXX

vba - 使用 Chromeoptions 在 Webdriver 中处理

javascript - 按属性值查找元素

python - 在函数之间传递参数的 pythonic 方式是什么?

python - 连接RDS上的MySQL数据库

java - Selenium - 如何在 Java 中正确选择元素?

python-3.x - 使用 chrome canary 执行 selenium python 脚本时如何抑制控制台错误/警告/信息消息

firefox - 如何使用 Java 在 Selenium WebDriver 中设置自动检测代理设置