我有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 & 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代码,它将查找页面中是否存在给定的字符串。
我正在尝试编写逻辑以提取与特定配置文件关联的锚链接(如果字符串与该配置文件(锚标签)相关联)。
我的python snnipet:
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
在包含文本'Organise vos evenements professionnels'
的p标签之前找到锚标签:
"//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/