将HTML数据转换为文本格式

时间:2019-04-28 17:26:46

标签: python selenium selenium-webdriver selenium-chromedriver

我正在使用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

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要先选择一个元素:

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