如何获得不确定的“< p>”使用零食的标签?

发布于 2025-02-10 10:17:36 字数 686 浏览 1 评论 0原文

如何使用scrapy获取不确定的< p>标签的文字?如以下两个示例所示:

获取所有< p>< h2> h2> xxxx特征</h2>or < h3< h3< /h3> < div class =“ entry-content”>的内部,然后将< p>的块合并到其他字段中,但< p>的数量尚不确定。

第1页 page2

How to get text of uncertain number of <p> tag using scrapy? as in the following two examples:

Get all <p> text after <h2>XXXX Characteristics</h2> or <h3>XXXX Diet</h3> inside of <div class="entry-content">, then merge block of <p> to other field, but the number of <P> is uncertain.

Page1
Page1
Page2
Page2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情绪失控 2025-02-17 10:17:36

您可以尝试提取div的所有孩子,并执行正则测试,以查看是否是h2h3,然后测试是否文本CONATINS “ Diet”“特征”,如果它通过,则收集所有兄弟姐妹,其中&lt; p&gt;

def parse(self, response):
    collect = False
    contents = []
    for selector in response.xpath("//div[@class='entry-content']/*"):
        val = selector.xpath("./text()").get()
        if collect and selector.re('<p'):
            contents.append(val)
            continue
        if val and selector.re(r'<h[23]'):
            if "Characteristics" in val or "Diet" in val:
                collect = True
        else:
            collect = False
    yield {"contents" : contents}

You can try extracting all the children of the div and perform a regex test to see if it is an h2 or h3 then test if the text conatins "Diet" or "Characteristics" and if it passes collect all siblings with that are <p>.

def parse(self, response):
    collect = False
    contents = []
    for selector in response.xpath("//div[@class='entry-content']/*"):
        val = selector.xpath("./text()").get()
        if collect and selector.re('<p'):
            contents.append(val)
            continue
        if val and selector.re(r'<h[23]'):
            if "Characteristics" in val or "Diet" in val:
                collect = True
        else:
            collect = False
    yield {"contents" : contents}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文