简体   繁体   English

使用 BeautifulSoup 获取属性文本的最佳方法

[英]Best approach to get attribute text with BeautifulSoup

What would be the best way to get the text of the items class="field__label" y class="field__item" in the following code在以下代码中获取项目文本的最佳方法是什么class="field__label" y class="field__item"

Taking into consideration that there are other tags with the same class outside the div class="fieldset-wrapper" I just need the ones inside this tag.考虑到在div class="fieldset-wrapper"之外还有其他具有相同类的标签,我只需要这个标签内的标签。

HTML Example: HTML 示例:

<div class="fieldset-wrapper">

  <div class="field field--name-field-adresse-strasse-nr field--type-string field--label-inline clearfix">
    <div class="field__label">TEXT</div>
    <div class="field__item">TEXT</div>
  </div>

  <div class="field field--name-field-adresse-plz-ort field--type-string field--label-inline clearfix">
    <div class="field__label">TEXT</div>
    <div class="field__item">TEXT</div>
  </div>

  <div class="field field--name-field-adressen-bundesland field--type-entity-reference field--label-inline clearfix">
    <div class="field__label">TEXT</div>
    <div class="field__item">TEXT</div>
  </div>

</div>

You can use css selectors to ensure that your target elements are descendants of the div class="fieldset-wrapper" element:您可以使用 css 选择器来确保您的目标元素是div class="fieldset-wrapper"元素的后代:

for item in  soup.select('div.fieldset-wrapper div.field__item, div.fieldset-wrapper  div.field__label'):
    print(item.text)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM