- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想构建以下 XML 元素(为了自定义数字格式):
<figcaption>
<span class="fignum">Figura 1.2</span> - Description of figure.
</figcaption>
但我不知道如何指定文本的位置。事实上,如果我在创建文本之前创建子元素,
import lxml.etree as et
fc = et.Element("figcaption")
fn = et.SubElement(fc, "span", {'class':'fignum'})
fn.text = "Figure 1.2"
fc.text = " - Description of figure."
我得到了不需要的结果(文本位于子元素之前):
<figcaption>
- Description of figure.<span class="fignum">Figure 1.2</span>
</figcaption>
如何指定文本相对于子元素的位置?
最佳答案
您需要使用 span
元素的 tail
属性:
from lxml import etree as et
fc = et.Element("figcaption")
fn = et.SubElement(fc, "span", {'class':'fignum'})
fn.text = "Figure 1.2"
fn.tail = " - Description of figure."
print(et.tostring(fc))
b'<figcaption><span class="fignum">Figure 1.2</span> - Description of figure.</figcaption>'
对于 ElementTree,元素在元素内部有一个 text
,在元素之后和外部有一个 tail
。
对于多个子元素,父元素的 text
是第一个子元素之前的文本,元素内的所有其他文本将分配给子元素的 tail
。
此问题的另一个答案中的一些示例现已删除:
<elem>.text of elem</elem>.tail of elem
<elem>.text of elem<child1/><child2/></elem>.tail of elem
<elem>.text of elem<child1/>.tail of child1<child2/>.tail of child2</elem>.tail of elem
<elem>.text of elem<child1>.text of child1</child1>.tail of child1<child2/>.tail of child2</elem>.tail of elem
关于Python lxml : insert text at given position relatively to subelements,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38520331/
我有以下xml文件 10.10.0.135::3111 10.10.0.130::3111 10.10.0.129::3111 10.
我已经为 PyPy 创建了一个 virtualenv: virtualenv test -p `which pypy` source test/bin/activate 我安装了以下依赖项: sudo
我正在尝试使用 lxml 的 ElementTree etree 在我的 xml 文档中查找特定标签。标签如下所示: 12 我希望使用 etree.find('text:statedAge
在 python 3.5 项目中,我必须读取一些 xml 文件,因此我决定使用 lxml 库。由于我正在阅读文件,因此根据文档执行此操作的最有效方法是使用 lxml.etree.parse(...)
执行 pip install lxml 或 pip install pyquery 会出现此错误: gcc: error trying to exec 'cc1': execvp: No such f
我正在使用下面的代码获取一个部分的所有 html 内容以保存到数据库 el = doc.get_element_by_id('productDescription') lxml.html.tostri
我想以可区分的方式打印出 etree 的树结构(由 html 文档形成)(意味着两个 etree 应该以不同的方式打印出来)。 我所说的结构是指树的“形状”,基本上是指所有标签,但没有属性,也没有文本
谁能解释为什么第一次调用 root.cssselect() 有效,而第二次失败? from lxml.html import fromstring from lxml import etree htm
我收到此错误“ImportError: No module named lxml”,即使确实安装了 LXML。具体来说,它安装在项目的 python Virtualenv 中。最终我正在研究 Pyth
我在 Windows 32 位上使用 Anaconda v4.2 和 Python 3.5,并想使用 lxml etree。我的 Anaconda 发行版包括 lxml 3.6.4,但我的 IDE(P
我有一个脚本,可以从 URL 列表的 XML 文件中提取一些术语。所有 URL 都可以访问 XML 数据。 它在第一次正确打开、解析和提取时工作正常,但随后在过程中被某些 XML 文件中断并出现此错误
我正在使用 mechanize/cookiejar/lxml 来读取页面,它适用于某些页面但不适用于其他页面。我在其中遇到的错误是标题中的错误。我不能在这里发布页面,因为它们不是 SFW,但是有没有办
这是一个基本的问题,我实际上在文档中找不到它:-/ 如下: img = house_tree.xpath('//img[@id="mainphoto"]')[0] 如何获取 的 HTML标记? 我尝
我只想说,这个问题我已经在Pip is already installed: but I am getting no module named lxml看到了并且已经看到关于以非 root 用户身份安
我通过 pip 安装了 lxml 3.3.5。现在我在运行一些 Django 测试时遇到了问题: Traceback (most recent call last): File "manage.p
我有一个 Django 应用程序,其中包含 Tastypie 提供的 RESTFull 服务。我使用 easy_install 安装 lxml 和 defusedxml,这样我就可以使用 xml 而不
我正在使用 lxml etree 创建 xml 或 REST 调用。我有命名空间的问题,因为如果没有正确制定,我会从服务器收到语法错误。 正如您在以下 2 个示例中所看到的,我应该得到例如 ns1、n
我对导航具有 lxml.etree namespace 的 xml 文档感到有些困惑。 .我已经看到了一些关于这个主题的主题( 1 、 2 )以及 lxml docs但仍然没有想出答案。 xml =
由于各种原因,我试图从lxml.html.fromstring()切换到lxml.html.html5parser.document_fromstring()。两者之间的最大区别是,第一个返回lxml
我需要通过向现有元素添加子元素来修改现有的 xml 文件。我使用 lxml 库。 Eric Idle 999-999-999 555-555-555
我是一名优秀的程序员,十分优秀!