【问题标题】:Find and retrieve content from html text using BeautifulSoup使用 BeautifulSoup 从 html 文本中查找和检索内容
【发布时间】:2018-11-10 19:08:14
【问题描述】:

我正在使用 Python 上的 BeautifulSoup 编写以下 html 代码(或者至少我认为它是 html)。

我已经正确地使用 Beautiful soup 解析了 html。我接下来要做的是检索与包含某个数据标签的“div”关联的内容(例如,在代码的底部,data-label="Relation")。特别是我想获得一个字典,它以数据标签的文本为键,即在我的示例中为“关系”,作为值,相同的“div”的内容,即在我的示例中为 href "@987654321 @"

我尝试了几种方法,但据我所知,data-label 似乎不是一个有效的属性,所以我不确定如何处理。

(请注意,这只是一个示例,但我必须对具有这种相似结构的数千个(如果不是数百万个)网页做同样的事情)。

感谢任何帮助。谢谢!

<div id="directs">
  <label class="c1"><a data-comment="A human-readable name for the subject." data-label="label" href="http://www.w3.org/2000/01/rdf-schema#label">
    rdfs:<span>label</span>
    </a></label>
  <div class="c2 value ">
    <div class="toMultiLine ">
      <div class="fixed">
        <span class="dType">xsd:string</span> 
        intervento di Fabrizio CICCHITTO
      </div>
    </div>
  </div>
  <label class="c1"><a data-comment="A name given to the resource." data-label="Title" href="http://purl.org/dc/elements/1.1/title">
      dc:<span>title</span>
    </a></label>
  <div class="c2 value ">
    <div class="toMultiLine ">
      <div class="fixed">
        intervento di Fabrizio CICCHITTO
      </div>
    </div>
  </div>
  <label class="c1"><a data-comment="" data-label="" href="http://lod.xdams.org/ontologies/ods/modified">
      ods:<span>modified</span>
    </a></label>
  <div class="c2 value ">
    <div class="toMultiLine ">
      <div class="fixed">
        <span class="dType">xsd:dateTime</span> 
        2016-07-05T12:26:02Z
      </div>
    </div>
  </div>
  <label class="c1"><a data-comment="The subject is an instance of a class." data-label="type" href="http://www.w3.org/1999/02/22-rdf-syntax-ns#type">
      rdf:<span>type</span>
    </a></label>
  <div class="c2 value">
    <div class="toOneLine">
      <a class=" isLocal" href="http://dati.camera.it/ocd/intervento" title="&lt;http://dati.camera.it/ocd/intervento&gt;">
      ocd:intervento
      </a>
    </div>
  </div>
  <label class="c1"><a data-comment="propriet generica utilizzata per puntare alla risorsa deputato in vari punti dell'ontologia" data-label="rierimento a deputato" href="http://dati.camera.it/ocd/rif_deputato">
      ocd:<span>rif_deputato</span>
    </a></label>
  <div class="c2 value">
    <div class="toOneLine">
      <a class=" isLocal" href="http://dati.camera.it/ocd/deputato.rdf/d15080_17" title="&lt;http://dati.camera.it/ocd/deputato.rdf/d15080_17&gt;">
      http://dati.camera.it/ocd/deputato.rdf/d15080_17
      </a>
    </div>
  </div>
  <label class="c1"><a data-comment="A related resource." data-label="Relation" href="http://purl.org/dc/elements/1.1/relation">
      dc:<span>relation</span>
    </a></label>
  <div class="c2 value">
    <div class="toOneLine">
      <a class=" " href="http://documenti.camera.it/apps/commonServices/getDocumento.ashx?sezione=bollettini=comunicato=17=2016=06=14=03=data.20160614.com03.bollettino.sede00020.tit00010.int00010=data.20160614.com03.bollettino.sede00020.tit00010.int00010#data.20160614.com03.bollettino.sede00020.tit00010.int00010"
        target="_blank" title="&lt;http://documenti.camera.it/apps/commonServices/getDocumento.ashx?sezione=bollettini=comunicato=17=2016=06=14=03=data.20160614.com03.bollettino.sede00020.tit00010.int00010=data.20160614.com03.bollettino.sede00020.tit00010.int00010#data.20160614.com03.bollettino.sede00020.tit00010.int00010&gt;">
         http://documenti.camera.it/apps/commonServices/getDocumento.ashx?sezione=bollettini=comunicato=17=2016=06=14=03=data.20160614.com03.bollettino.sede00020.tit00010.int00010=data.20160614.com03.bollettino.sede00020.tit00010.int00010#data.20160614.com03.bollettino.sede00020.tit00010.int00010
        </a>
    </div>
  </div>
</div>

【问题讨论】:

    标签: python html web-scraping beautifulsoup


    【解决方案1】:

    您可以在一个通道中找到data-labels,在另一个通道中找到div 内容。然后,可以将结果压缩在一起以创建字典:

    from bs4 import BeautifulSoup as soup
    import re
    d = soup(content, 'html.parser').find('div', {'id':'directs'})
    _labels = [i.a['data-label'] for i in d.find_all('label')]
    _content = [i.text for i in d.find_all('div', {'class':re.compile('c2 value\s*')})]
    result = dict(zip(_labels, _content))
    

    输出:

    {'label': '\n\n\nxsd:string \n        intervento di Fabrizio CICCHITTO\n      \n\n', 
     'Title': '\n\n\n        intervento di Fabrizio CICCHITTO\n      \n\n', 
     '': '\n\n\nxsd:dateTime \n        2016-07-05T12:26:02Z\n      \n\n', 
     'type': '\n\n\n      ocd:intervento\n      \n\n', 
     'rierimento a deputato': '\n\n\n      http://dati.camera.it/ocd/deputato.rdf/d15080_17\n      \n\n', 
      'Relation': '\n\n\n         http://documenti.camera.it/apps/commonServices/getDocumento.ashx?sezione=bollettini=comunicato=17=2016=06=14=03=data.20160614.com03.bollettino.sede00020.tit00010.int00010=data.20160614.com03.bollettino.sede00020.tit00010.int00010#data.20160614.com03.bollettino.sede00020.tit00010.int00010\n        \n\n'}
    

    【讨论】:

    • @mgiom 很高兴为您提供帮助!
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 2020-01-04
    • 2023-03-16
    • 2020-04-01
    • 2013-05-23
    • 2016-07-06
    • 2014-03-27
    相关资源
    最近更新 更多