从降序元素中选择文本和该元素

2024-04-25 06:26:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从降序元素和元素本身中选择文本。什么xpath选择器可以做到这一点?我如何组合所有文本部分?你知道吗


Tags: 文本元素选择器xpath降序
1条回答
网友
1楼 · 发布于 2024-04-25 06:26:51

你要找的是//text()。你知道吗

拍摄于from w3schools

// - Selects nodes in the document from the current node that match the selection no matter where they are

示例:

$ scrapy shell
In [1]: data = """
   ...: <div> root text
   ...: <div> level 2 text </div>
   ...: <div> level 2 text2 </div>
   ...: </div>
   ...: """
In [2]: from scrapy import Selector
In [3]: sel = Selector(text=data)
In [4]: sel.xpath("//div//text()").extract()
Out[4]: [u' root text\n', u' level 2 text ', u'\n', u' level 2 text2 ', u'\n']

相关问题 更多 >