2017-10-13 81 views
1

到目前爲止,我所看到的是,如果通過硒進行過濾,網頁的頁面源代碼就可以從該頁面源解析文本或必需的東西,並應用bs4或lxml不管頁面源是否啓用了JavaScript。不過,我的問題是,如何通過過濾硒然後使用bs4或lxml庫來解析來自某個html elements的文檔。如果粘貼下面元素被認爲然後將BS4或限於lxml我的移動方式是:使用硒解析來自某些「html元素」的文本

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 

現在,我怎麼可以過濾上述糊html部分使用硒,然後將其應用於BS4庫?無法想到driver.page_source,因爲它僅適用於從網頁過濾的情況。

爲了更具體一點,如果我想使用類似下面的東西,那該怎麼做?

from selenium import webdriver 
driver = webdriver.Chrome() 

element_html = driver-------(html) #this "html" is the above pasted one 
print(element_html) 

回答

1

driver.page_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重新解析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()) 

打印:

License: 20-214767 (Validity: 21/05/2022)20C-214769 (Validity: 21/05/2022)21-214768 (Validity: 21/05/2022) 
+0

感謝alecxe你的答案爵士。這是我無法提供我所期望的清晰度。現在它更有意義,我期望。謝謝。 – SIM

+0

您可能已經注意到,上面粘貼的html元素中的'span'標籤在javascript中,這就是爲什麼我會在這種用法之後。再次感謝。 – SIM

+0

@Topto啊,現在我發現它位於'onmouseover'屬性中。我會提供一個關於如何使用bs4提取它的示例,給我一分鐘。 – alecxe