1,首先使用selenium xpath

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from lxml import etree

#下面代码主要是让selenium使用无界面的chrome浏览器
chrome_options = Options()    
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(chrome_options=chrome_options)

driver.get(url)
#当用driver使用get_attribute时,获取到的是整个column标签下面所有的html,是字符串格式----不对etree对象有用
column = driver.find_element_by_class_name('column').get_attribute(
'innerHTML')
html = etree.HTML(column)   #使用etree变成lxml格式
html.xpath('//li[@class="first_f"]//div[@class="msg"]/a[2]/text()')
# 获取到的值是文本['内容'],列表格式的字符串()

如果要获取标签里面的html,
details = html.xpath('//li[@) # 获取到的是标签的html,是byte类型
details = details.decode('utf-8') if details else None       # 返回字符串格式

 

相关文章:

  • 2021-05-05
  • 2021-10-28
  • 2021-10-26
  • 2021-10-25
  • 2022-12-23
  • 2021-10-19
  • 2022-02-27
  • 2021-12-01
猜你喜欢
  • 2022-12-23
  • 2022-01-18
  • 2021-06-17
  • 2022-12-23
  • 2021-08-03
  • 2021-09-26
  • 2021-04-28
相关资源
相似解决方案