使用 ElementTree/lxml 查找文本字符串的父标签
我正在尝试获取一串文本,并从 html 中“提取”段落/文档中的其余文本。
我当前的方法是尝试在已用 lxml 解析的 html 中找到字符串的“父标记”。 (如果您知道解决此问题的更好方法,我会洗耳恭听!)
例如,在树中搜索“TEXT STRING HERE”并返回“p”标签。 (请注意,我不会事先知道 html 的确切布局)
<html>
<head>
...
</head>
<body>
....
<div>
...
<p>TEXT STRING HERE ......</p>
...
</html>
感谢您的帮助!
I'm trying to take a string of text, and "extract" the rest of the text in the paragraph/document from the html.
My current is approach is trying to find the "parent tag" of the string in the html that has been parsed with lxml. (if you know of a better way to tackle this problem, I'm all ears!)
For example, search the tree for "TEXT STRING HERE" and return the "p" tag. (note that I won't know the exact layout of the html beforehand)
<html>
<head>
...
</head>
<body>
....
<div>
...
<p>TEXT STRING HERE ......</p>
...
</html>
Thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是使用 ElementTree 实现此目的的简单方法。 它确实要求您的 HTML 输入是有效的 XML(因此我已向您的 HTML 添加了适当的结束标记):
This is a simple way to do it with ElementTree. It does require that your HTML input is valid XML (so I have added the appropriate end tags to your HTML):