【问题标题】:How to extract a nested tag?如何提取嵌套标签?
【发布时间】:2022-11-19 15:34:14
【问题描述】:

我想从 'p' 中提取 'span' 标签,但我不知道该怎么做

html = "
<div id="tab-description" class="plugin-description section">
    <h2 id="description-header">Description</h2>
    <p><span class="embed-youtube" style="text-align:center; display: block;"><iframe class="youtube-player"src="https://www.youtube.com/"></iframe></span></p>
</div>
"
soup = BeautifulSoup(html,'lxml')
description = soup.find(id="tab-description").find('p')

我尝试decompose()它但返回错误。

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    要获取 &lt;span&gt; 直接选择它:

    soup.find(id="tab-description").p.span
    

    或者

    soup.find(id="tab-description").find('span')
    

    或者

    soup.select_one('#tab-description p > span')
    

    意识到 不是从 &lt;iframe&gt; 中抓取内容的选项,如果这应该是意图的话。如果是这样?这将是命中注定的 asking a new question 的焦点。

    【讨论】:

      猜你喜欢
      • 2012-08-19
      • 2019-11-06
      • 2021-12-28
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 2015-07-30
      • 2014-05-11
      • 1970-01-01
      相关资源
      最近更新 更多