【问题标题】:Scrapy - Python - Scraping all text with p in a divScrapy - Python - 在 div 中使用 p 抓取所有文本
【发布时间】:2018-10-17 22:16:47
【问题描述】:

我正在尝试从此 div 中抓取文本

<div class="col-lg-6">
    <h3 class="c-panel__section-heading">Reply</h3>
    <div class="textAreaContainer closed">

        <div contenteditable="true" class="customTextArea" id="Message" name="Message">
            <p>Dear Customer,&nbsp;</p>
            <p>the&nbsp;<span style="background-color: rgb(238, 238, 238);">MFDeviceMT.dll is a Matrox driver related dll, if you're not using a MATROX card on the server where you encountered the issue you can temporarily ignore it.</span></p>
            <p><span style="background-color: rgb(238, 238, 238);">We have however forwarder the problem to our developing team, thank you for the feedback.</span></p>
            <p><span style="background-color: rgb(238, 238, 238);">Best Regards.</span></p>
            -------------- -------------- ----------- Email send to: martin.bonato@brasvideo.com;b2w.shoptime@brasvideo.com Email send cc: supporto@etere.com
        </div>
    </div>
</div>

但现在网站实现了标签

我不能刮掉所有的文字 我正在使用这个命令

sel.xpath('//*[@id="Message"]/text()').extract()[-1]

然后它返回

最后一个p标签中的所有文字

那么我怎样才能用 p 标签刮掉 div 中的所有文本

【问题讨论】:

    标签: python html scrapy


    【解决方案1】:

    你想单独抓取ps 的所有文本吗?遍历它们

    for p in sel.css('#Message p'):
       all_text = "".join(p.css("*::text").extract())
    

    【讨论】:

      【解决方案2】:

      我是这样做的

      sel.xpath('//*[@id="solutionsContainer"]/div[last()]/div[last()]/div//text()').extract()
      for i_msg in ultima_msg:
                  limpa_msg = limpa_msg + i_msg.strip()
      

      我认为你的方法更简单

      谢谢大家

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-07-01
        • 1970-01-01
        • 2016-01-06
        • 2018-06-09
        • 2017-11-05
        • 2014-03-02
        • 1970-01-01
        • 2015-12-30
        相关资源
        最近更新 更多