【问题标题】:How to extract text that has no CSS selector with Python如何使用 Python 提取没有 CSS 选择器的文本
【发布时间】:2021-03-27 21:36:20
【问题描述】:

因此,我目前正在为文本抓取几个网页,然后将其放置在列表中,然后我可以通过 Pandas 在数据框中组织这些文本。我目前正在使用requestsbs4 模块。 我需要抓取的文本之一是这种形式:

<li><span class="label-description">STATUS</span><span class="text-description"></span>Ongoing</li>

我需要提取“正在进行的”,但它没有我可以用来使用soup.select(selector) 的 CSS 选择器。 我能做什么?

【问题讨论】:

    标签: python web-scraping beautifulsoup


    【解决方案1】:

    您可以从.contents&lt;li&gt; 标签中选择最后一个元素:

    print(soup.find("li").contents[-1])
    

    打印:

    Ongoing
    

    或者来自class="text-description"的下一个文本元素:

    print(soup.find(class_="text-description").find_next(text=True))
    

    【讨论】:

      【解决方案2】:

      直接按类选择呢?

      soup.select(".text-description")
      

      【讨论】:

      • @QHarr 你说得对,我已经澄清了,因为我怀疑 OP 正在从开发人员工具中复制“CSS 选择器”
      猜你喜欢
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 2019-10-11
      • 2021-12-31
      • 2014-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      相关资源
      最近更新 更多