删除指定的元素标签
from lxml import etree
import html
def delete_e(d,old_xpath,xpath_list):
'''
:param d: 要删除html标签
:param old_xpath: 最后一级定位标签
:param xpath_list: 要删除的标签元素集合
:return: 返回删除后的标签元素
'''
content= etree.HTML(d)
for a in xpath_list:
try:
delem=content.xpath(a)[0]
delem.getparent().remove(delem)
except:
pass
content = content.xpath(old_xpath)[0]
content = etree.tostring(content, pretty_print=True).decode('utf-8')
desc = html.unescape(content)
return desc
例子:
content = response.xpath(’//div[@id=“zoom”]’).extract_first()
<!-- 假设content的html结构如下:-->
<div id='zoom'>
<p> p标签1 </p>
<p> p标签2 </p>
<p> p标签3 </p>
<div> div 标签1 </div>
<div> div 标签2 </div>
<span> span标签 </span>
<di>
<!-- 此时,假设要删除p标签3,使用方法如下:
只需将xpath定位取出的标签代码赋值给d,将xpath语句的最后一级赋值给old_xpath,
注意:content的xpath语句只有一级,也就只有一个div,所有不用更改,若xpath语句是 //div[@id="zoom"]/div,则赋值给old_xpath的是'//div'-->
content = delete_e(d=content, old_xpath=’//div[@id=“zoom”]’, xpath_list=[’//p[3]’])