使用BeautifulSoup从html中提取元素

问题描述 投票:0回答:1

我有selenium beautifulsoup自动化脚本,可以使用应用程序密码访问域电子邮件,如outlook、gmail、aol、yahoo ...。

我想从电子邮件正文中提取(设备 IMEI 号码:)和(请求号码:)。

html代码

<br>
<br>
<b>Device IMEI number:</b>
<span style="color: #1D2329 !important; text-decoration:none;">
<a style="color: #1D2329;font-family:AleckSansfont-regular, Arial, Helvetica, sans-serif; text-decoration:none;">
353779334398833</a>
</span>
<br>
<b>Request number:</b>
<span style="color: #1D2329 !important; text-decoration:none;">
<a style="color: #1D2329;font-family:AleckSansfont-regular, Arial, Helvetica, sans-serif; text-decoration:none;">
NUL836822403006</a>
</span>

电子邮件正文和 html 代码的照片:

beautifulsoup
1个回答
0
投票

这应该有效:

美丽的汤

imei = soup.find('b', string='Device IMEI number:').find_next_sibling('span').get_text(strip=True)
request_number = soup.find('b', string='Request number:').find_next_sibling('span').get_text(strip=True)

imei = driver.find_element(By.XPATH, '//b[text()="Device IMEI number:"]/following-sibling::span[1]/a').text
request_number = driver.find_element(By.XPATH, '//b[text()="Request number:"]/following-sibling::span[1]/a').text
© www.soinside.com 2019 - 2024. All rights reserved.