匹配scray中的多个<p>标记

2024-04-26 13:08:50 发布

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

我有如下内容html

<div class="articleBody">
  <p>
    <strong>Text</strong> lorem ipsum... 
    <strong>lorem ipsum...</strong>
  </p>
  <p>lorem ipsum 
    <strong> lorem ipsum lorem ipsum</strong>
    lorem ipsum...lorem ipsum...lorem ipsum...lorem ipsum...
  </p>
</div>

更一般地说,我有一个<p>标记的列表,其中有一些<strong>标记。在

我想得到所有<p>标记的文本,减去<strong>标记。。。我指的是“articleBody”div类中的文本。在

我所拥有的是

^{pr2}$

但它只返回第一个<p>。在

任何帮助都将不胜感激。在


Tags: text标记文本div内容列表htmlclass
1条回答
网友
1楼 · 发布于 2024-04-26 13:08:50

试试看:

for node in response.xpath('//div[@class="articleBody"]//p'):
        print node.xpath('string()').extract()

…然后你可以连接你的字符串或将它们添加到一个列表或其他任何东西,而不是像我那样只打印它们。在

xpath 2.0也有string-join()函数,但看起来scray支持xpath 1.0。在

有关字符串联接等的详细信息:http://www.w3.org/TR/xpath-functions/#func-string-join

相关问题 更多 >