Skip to main content
 首页 » 编程设计

python之将 HTML 数据转换为文本格式

2024年11月24日10JustinYoung

我正在使用 Selenium Web Driver 从 LinkedIn 个人资料中提取数据点。在这个例子中,我想从技能部分提取每项技能,但数据被提取为 HTML 格式。

当尝试将 HTML 代码转换为文本时,我收到了附加的错误消息。

from parsel import Selector   
from selenium import webdriver  
from selenium.webdriver.common.keys import Keys   
from bs4 import BeautifulSoup  
 
driver = webdriver.Chrome('/Users/davidcraven/Downloads/chromedriver') 
 
# get profile URL 
driver.get('https://www.linkedin.com/AnyProfileURL') 
 
# assigning the source code for the web page to variable sel 
sel = Selector(text=driver.page_source) 
 
# get skills 
skills = sel.xpath('//*[starts-with(@class, "skills searchable has-several ")]').extract() 
 
newtext = BeautifulSoup(skills, "lxml").text 

请您参考如下方法:

你需要先选择一个元素:

driver.get('https://www.linkedin.com/AnyProfileURL') 
soup = BeautifulSoup(driver.page_source, "lxml") 
elem = soup.select_one('.skills.searchable.has-several') 
if elem: 
    txt = elem.text