本文介绍了如何在lxml.html的树中插入HTML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用的是python3.3和lxml 3.2.0
问题:我有一个变量webpageString = "<html><head></head><body>webpage content</body></html>"
中的网页我想在两个头标记之间插入一个css链接标记,这样我就可以得到webpageString = "<html><head><link rel='stylesheet' type='text/css'></head><body>webpage content</body></html>"
我编写了以下代码:
def addCssCode(self):
tree = html.fromstring(self.article)
headTag = tree.xpath("//head")
#htmlTag = tree.getroot()
if headTag is None:
pass #insert the head tag first
cssLinkString = "<link rel='stylesheet' type='text/css' href='"+ self.cssLocation+"'>"
headTag[0].insert(1, html.HtmlElement(cssLinkString))
print(cssLinkString)
self.article = html.tostring(tree).decode("utf-8")
这会导致插入-
<HtmlElement>< link rel='stylesheet' type='text/css' href='cssCode.css' ></HtmlElement>
我也尝试了以下页面中的解决方案来解决相同的问题,但也不起作用。python lxml append element after another element
我如何才能解决这个问题?谢谢
推荐答案
使用.insert
/.append
方法。
import lxml.html
def add_css_code(webpageString, linkString):
root = lxml.html.fromstring(webpageString)
link = lxml.html.fromstring(linkString).find('.//link')
head = root.find('.//head')
title = head.find('title')
if title == None:
where = 0
else:
where = head.index(title) + 1
head.insert(where, link)
return lxml.html.tostring(root)
webpageString1 = "<html><head><title>test</title></head><body>webpage content</body></html>"
webpageString2 = "<html><head></head><body>webpage content</body></html>"
linkString = "<link rel='stylesheet' type='text/css'>"
print(add_css_code(webpageString1, linkString))
print(add_css_code(webpageString2, linkString))
这篇关于如何在lxml.html的树中插入HTML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!