gpt4 book ai didi

python - 如何在 lxml.html 树中插入 HTML 元素

转载 作者:行者123 更新时间:2023-11-28 18:48:17 27 4
gpt4 key购买 nike

我正在使用 python 3.3 和 lxml 3.2.0

问题:我在变量 webpageString = "<html><head></head><body>webpage content</body></html>" 中有一个网页我想在两个 header 标签之间插入一个 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>&lt; link rel='stylesheet' type='text/css' href='cssCode.css' &gt;</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))

关于python - 如何在 lxml.html 树中插入 HTML 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17235660/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com