繁体   English   中英

在lxml中解析HTML树:如何检索元素内的文本?

[英]Parsing HTML tree in lxml : how can I retrieve the text inside the element?

我正在尝试检索元素内的正确文本。 这是输出:

(Pdb) p etree.tostring(els[0])
'<h5 class="msg-delivered" style="padding:0;text-rendering:optimizeLegibility;line-height:1.1;margin-bottom:15px;-webkit-font-smoothing:antialiased;font-family:&quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, Arial, Helvetica, sans-serif;color:#888888;vertical-align:middle;margin:0;font-size:13px;font-weight:300 !important">&#13;\n<i class="ic-icon-delivered" style="margin:0;padding:0;font-family:&quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;text-rendering:optimizeLegibility;position:relative;background:url(https://d1s8987jlndkbs.cloudfront.net/assets/sprite-ratings-ee0696744f54df6536179c70e24217e3.png) no-repeat -12px -12px;background-size:132px 436px;display:none;vertical-align:middle;width:25px;height:25px;background-position:-16px -16px;top:0"/>&#13;\nYour order was delivered&#13;\non&#13;\n6/4&#13;\n@&#13;\n4:44 PM&#13;\n</h5>&#13;\n'
(Pdb) p els[0].text
'\r\n'

如何获取字符串:“您的商品在6/4下午4:40交付”? 我可以在etree.tostring()输出上使用正则表达式,但想知道为什么els [0] .text选项不起作用?

您可以尝试使用xpath函数string()返回当前元素内所有文本节点的串联值:

import lxml.html
html = """<h5 class="msg-delivered" style="padding:0;text-rendering:optimizeLegibility;line-height:1.1;margin-bottom:15px;-webkit-font-smoothing:antialiased;font-family:&quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, Arial, Helvetica, sans-serif;color:#888888;vertical-align:middle;margin:0;font-size:13px;font-weight:300 !important">&#13;\n<i class="ic-icon-delivered" style="margin:0;padding:0;font-family:&quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;text-rendering:optimizeLegibility;position:relative;background:url(https://d1s8987jlndkbs.cloudfront.net/assets/sprite-ratings-ee0696744f54df6536179c70e24217e3.png) no-repeat -12px -12px;background-size:132px 436px;display:none;vertical-align:middle;width:25px;height:25px;background-position:-16px -16px;top:0"/>&#13;\nYour order was delivered&#13;\non&#13;\n6/4&#13;\n@&#13;\n4:44 PM&#13;\n</h5>"""
tree = lxml.html.etee.fromstring(html)
print(tree.xpath("string()"))

输出:

'\r\n\r\nYour order was delivered\r\non\r\n6/4\r\n@\r\n4:44 PM\r\n'

如果您想要所有文本,则可以简单地使用:

els[0].text_content()

也就是说,假设您使用以下方式加载了html:

import lxml.html
html = """<h5 class="msg-delivered" style="padding:0;text-rendering:optimizeLegibility;line-height:1.1;margin-bottom:15px;-webkit-font-smoothing:antialiased;font-family:&quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, Arial, Helvetica, sans-serif;color:#888888;vertical-align:middle;margin:0;font-size:13px;font-weight:300 !important">&#13;\n<i class="ic-icon-delivered" style="margin:0;padding:0;font-family:&quot;Open Sans&quot;, &quot;Helvetica Neue&quot;, &quot;Helvetica&quot;, Helvetica, Arial, sans-serif;text-rendering:optimizeLegibility;position:relative;background:url(https://d1s8987jlndkbs.cloudfront.net/assets/sprite-ratings-ee0696744f54df6536179c70e24217e3.png) no-repeat -12px -12px;background-size:132px 436px;display:none;vertical-align:middle;width:25px;height:25px;background-position:-16px -16px;top:0"/>&#13;\nYour order was delivered&#13;\non&#13;\n6/4&#13;\n@&#13;\n4:44 PM&#13;\n</h5>"""
tree = lxml.html.fromstring(html)

请注意,您可能希望避免使用lxml.html。 etree .fromstring,只需使用lxml.html.fromstring

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM