Kev*_*rke 64 python parsing lxml
我想编写一个代码片段,它将<content>在下面所有三个实例(包括代码标记)中的lxml中获取标记内的所有文本.我已经尝试了tostring(getchildren())但是会遗漏标签之间的文字.我没有太多运气在API中搜索相关功能.你能救我吗?
<!--1-->
<content>
<div>Text inside tag</div>
</content>
#should return "<div>Text inside tag</div>
<!--2-->
<content>
Text with no tag
</content>
#should return "Text with no tag"
<!--3-->
<content>
Text outside tag <div>Text inside tag</div>
</content>
#should return "Text outside tag <div>Text inside tag</div>"
Run Code Online (Sandbox Code Playgroud)
小智 66
text_content()是否满足您的需求?
Art*_*ert 58
只需使用该node.itertext()方法,如:
''.join(node.itertext())
Run Code Online (Sandbox Code Playgroud)
alb*_*tov 41
尝试:
def stringify_children(node):
from lxml.etree import tostring
from itertools import chain
parts = ([node.text] +
list(chain(*([c.text, tostring(c), c.tail] for c in node.getchildren()))) +
[node.tail])
# filter removes possible Nones in texts and tails
return ''.join(filter(None, parts))
Run Code Online (Sandbox Code Playgroud)
例:
from lxml import etree
node = etree.fromstring("""<content>
Text outside tag <div>Text <em>inside</em> tag</div>
</content>""")
stringify_children(node)
Run Code Online (Sandbox Code Playgroud)
生产: '\nText outside tag <div>Text <em>inside</em> tag</div>\n'
ana*_*ana 16
的Albertov的一个版本字符串化内容,解决了该漏洞通过hoju报道:
def stringify_children(node):
from lxml.etree import tostring
from itertools import chain
return ''.join(
chunk for chunk in chain(
(node.text,),
chain(*((tostring(child, with_tail=False), child.tail) for child in node.getchildren())),
(node.tail,)) if chunk)
Run Code Online (Sandbox Code Playgroud)
定义stringify_children这种方式可能不那么复杂:
from lxml import etree
def stringify_children(node):
s = node.text
if s is None:
s = ''
for child in node:
s += etree.tostring(child, encoding='unicode')
return s
Run Code Online (Sandbox Code Playgroud)
或在一行
return (node.text if node.text is not None else '') + ''.join((etree.tostring(child, encoding='unicode') for child in node))
Run Code Online (Sandbox Code Playgroud)
基本原理与此答案相同:将子节点的序列化留给 lxml。在这种情况下的tail部分node并不有趣,因为它在结束标记的“后面”。请注意,encoding可以根据需要更改参数。
另一种可能的解决方案是序列化节点本身,然后去除开始和结束标记:
def stringify_children(node):
s = etree.tostring(node, encoding='unicode', with_tail=False)
return s[s.index(node.tag) + 1 + len(node.tag): s.rindex(node.tag) - 2]
Run Code Online (Sandbox Code Playgroud)
这有点可怕。仅当node没有属性时,此代码才是正确的,我认为即使到那时也没有人愿意使用它。
最简单的代码片段之一,实际上对我有用,并且根据http://lxml.de/tutorial.html#using-xpath-to-find-text的文档是
etree.tostring(html, method="text")
Run Code Online (Sandbox Code Playgroud)
其中 etree 是您正在尝试读取其完整文本的节点/标签。但请注意,它并没有摆脱脚本和样式标签。
import urllib2
from lxml import etree
url = 'some_url'
Run Code Online (Sandbox Code Playgroud)
获取网址
test = urllib2.urlopen(url)
page = test.read()
Run Code Online (Sandbox Code Playgroud)
获取包含表标签的所有html代码
tree = etree.HTML(page)
Run Code Online (Sandbox Code Playgroud)
xpath 选择器
table = tree.xpath("xpath_here")
res = etree.tostring(table)
Run Code Online (Sandbox Code Playgroud)
res 是表的 html 代码,这是为我做的工作。
因此您可以使用 xpath_text() 提取标签内容,并使用 tostring() 提取包括其内容的标签
div = tree.xpath("//div")
div_res = etree.tostring(div)
Run Code Online (Sandbox Code Playgroud)
text = tree.xpath_text("//content")
Run Code Online (Sandbox Code Playgroud)
或 text = tree.xpath("//content/text()")
div_3 = tree.xpath("//content")
div_3_res = etree.tostring(div_3).strip('<content>').rstrip('</')
Run Code Online (Sandbox Code Playgroud)
使用 strip 方法的最后一行并不好,但它只是有效
| 归档时间: |
|
| 查看次数: |
76584 次 |
| 最近记录: |