[英]How to insert a HTML element in a tree of lxml.html
I am using python 3.3 and lxml 3.2.0 我使用的是python 3.3和lxml 3.2.0
Problem: I have a web page in a variable webpageString = "<html><head></head><body>webpage content</body></html>"
And I want to insert a css link tag between the two header tags, so that I get webpageString = "<html><head><link rel='stylesheet' type='text/css'></head><body>webpage content</body></html>"
问题:我在变量webpageString = "<html><head></head><body>webpage content</body></html>"
有一个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>"
I have written the following code: 我写了以下代码:
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")
Which results in insertion of- 导致插入 -
<HtmlElement>< link rel='stylesheet' type='text/css' href='cssCode.css' ></HtmlElement>
I also tried solution in the following page to an identical problem, but it also didn't work. 我也尝试在下一页解决相同的问题,但它也没有用。 python lxml append element after another element python lxml追加另一个元素之后的元素
How can I solve this? 我怎么解决这个问题? Thanks 谢谢
Use .insert
/ .append
method. 使用.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))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.