Selenium 解析来自特定“的文本”;“html元素”;使用硒

Selenium 解析来自特定“的文本”;“html元素”;使用硒,selenium,selenium-webdriver,web-scraping,Selenium,Selenium Webdriver,Web Scraping,到目前为止,我所看到的是,如果网页的页面源通过selenium过滤,那么无论页面源是否启用javascript,都可以使用bs4或lxml解析该页面源中的文本或其他必要内容。然而,我的问题是如何通过过滤selenium,然后使用bs4或lxml库来解析来自某个html元素的文档。如果考虑以下粘贴元素,则应用bs4或lxml的移动方式为: html=''' <tr onmouseover="this.originalstyle=this.style.backgroundColor;this.

到目前为止,我所看到的是,如果网页的页面源通过selenium过滤,那么无论页面源是否启用javascript,都可以使用bs4或lxml解析该页面源中的文本或其他必要内容。然而,我的问题是如何通过过滤selenium,然后使用bs4或lxml库来解析来自某个
html元素的文档。如果考虑以下粘贴元素,则应用bs4或lxml的移动方式为:

html='''
<tr onmouseover="this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='DodgerBlue';
this.originalcolor=this.style.color;this.style.color='White';Tip('<span Style=Color:Red>License: <BR />20-214767 (Validity: 21/05/2022)<BR />20C-214769 (Validity: 21/05/2022)<BR />21-214768 (Validity: 21/05/2022)</span>');" onmouseout="this.style.backgroundColor=this.originalstyle;this.style.color=this.originalcolor;UnTip();" style="background-color:White;font-family:Times New Roman;font-size:12px;">
        <td style="font-size:10px;font-weight:normal;font-style:normal;text-decoration:none;" align="left">AAYUSH PHARMA</td><td style="font-size:10px;font-weight:normal;font-style:normal;text-decoration:none;" align="left">PUNE-1ST FLOOR, SR.NO.742/A, DINSHOW APARTMENT,,SWAYAM HOSPITAL AND NURSING HOME, BHAWANI PETH</td><td style="font-weight:normal;font-style:normal;text-decoration:none;" align="center">RH - 3</td><td>swapnil ramakant pawar, BPH, [140514-21/04/2017]</td>
</tr>
'''
from bs4 import BeautifulSoup
soup = BeautifulSoup(html,"lxml")
#rest of the code here

from lxml.html import fromstring
tree = fromstring(html)           
#rest of the code here

driver.page\u source
将在某个特定时刻为您提供页面的完整HTML源代码。但是,您拥有一个元素实例,可以使用
.get_attribute()
方法访问它的
outerHTML

element = driver.find_element_by_id("some_id")
element_html = element.get_attribute("outerHTML")

soup = BeautifulSoup(element_html, "lxml")
至于从
mouseover
属性中提取
span
元素源,我将首先使用
BeautifulSoup
解析
tr
元素,获取
onmouseover
属性,然后使用正则表达式从
Tip()
函数调用中提取html值。然后,使用
BeautifulSoup
重新解析span html:

import re

from bs4 import BeautifulSoup

html='''
<tr onmouseover="this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='DodgerBlue';
this.originalcolor=this.style.color;this.style.color='White';Tip('<span Style=Color:Red>License: <BR />20-214767 (Validity: 21/05/2022)<BR />20C-214769 (Validity: 21/05/2022)<BR />21-214768 (Validity: 21/05/2022)</span>');" onmouseout="this.style.backgroundColor=this.originalstyle;this.style.color=this.originalcolor;UnTip();" style="background-color:White;font-family:Times New Roman;font-size:12px;">
        <td style="font-size:10px;font-weight:normal;font-style:normal;text-decoration:none;" align="left">AAYUSH PHARMA</td><td style="font-size:10px;font-weight:normal;font-style:normal;text-decoration:none;" align="left">PUNE-1ST FLOOR, SR.NO.742/A, DINSHOW APARTMENT,,SWAYAM HOSPITAL AND NURSING HOME, BHAWANI PETH</td><td style="font-weight:normal;font-style:normal;text-decoration:none;" align="center">RH - 3</td><td>swapnil ramakant pawar, BPH, [140514-21/04/2017]</td>
</tr>
'''

soup = BeautifulSoup(html, "lxml")
mouse_over = soup.tr['onmouseover']

span = re.search(r"Tip\('(.*?)'\)", mouse_over).group(1)
span_soup = BeautifulSoup(span, "lxml")
print(span_soup.get_text())

您可能注意到,上面粘贴的html元素中的
span
标记位于javascript中,这就是为什么我希望使用这种方法。再次感谢。@Topto啊,现在我看到它位于
onmouseover
属性中。我将提供一个关于如何使用bs4提取它的样本,给我一分钟。@Topto更新了一个可复制的样本,希望这就是你要问的。谢谢。先生,您能给我提供任何我可以到达某个地方学习正则表达式的链接跟踪吗?@Topto我可能会从这里开始。然后,不断地练习——这是一种非常通用和有用的技能(尽管有时过度使用:)。
import re

from bs4 import BeautifulSoup

html='''
<tr onmouseover="this.originalstyle=this.style.backgroundColor;this.style.backgroundColor='DodgerBlue';
this.originalcolor=this.style.color;this.style.color='White';Tip('<span Style=Color:Red>License: <BR />20-214767 (Validity: 21/05/2022)<BR />20C-214769 (Validity: 21/05/2022)<BR />21-214768 (Validity: 21/05/2022)</span>');" onmouseout="this.style.backgroundColor=this.originalstyle;this.style.color=this.originalcolor;UnTip();" style="background-color:White;font-family:Times New Roman;font-size:12px;">
        <td style="font-size:10px;font-weight:normal;font-style:normal;text-decoration:none;" align="left">AAYUSH PHARMA</td><td style="font-size:10px;font-weight:normal;font-style:normal;text-decoration:none;" align="left">PUNE-1ST FLOOR, SR.NO.742/A, DINSHOW APARTMENT,,SWAYAM HOSPITAL AND NURSING HOME, BHAWANI PETH</td><td style="font-weight:normal;font-style:normal;text-decoration:none;" align="center">RH - 3</td><td>swapnil ramakant pawar, BPH, [140514-21/04/2017]</td>
</tr>
'''

soup = BeautifulSoup(html, "lxml")
mouse_over = soup.tr['onmouseover']

span = re.search(r"Tip\('(.*?)'\)", mouse_over).group(1)
span_soup = BeautifulSoup(span, "lxml")
print(span_soup.get_text())
License: 20-214767 (Validity: 21/05/2022)20C-214769 (Validity: 21/05/2022)21-214768 (Validity: 21/05/2022)