【问题标题】:Beautiful Soup - Loop Through Navigable StringBeautiful Soup - 遍历可导航字符串
【发布时间】:2016-05-25 15:25:29
【问题描述】:

我正在尝试遍历 NavigableString 并获取“a”标签中的所有内容。我做了一个

home= items.findAll('div', class_="dlineups-half") 

它让我得到下面的 NaviableString。我可以通过 home.a.contents 获取第一条数据,这给了我 Joe Bendik,但是每当我运行任何类型的循环来获取所有 .a.contents 时,我都会得到一个没有属性的“NavigableString”对象。我的问题是如何遍历 NavigableString 并获取所有 a.contents。

<div class="dlineups-half"><div class="dlineups-vplayer"><div
class="dlineups-pos">G</div><div><a href="/soccer/player.htm?id=18688"
  title="Joe Bendik">Joe Bendik</a></div><div class="dlineups-vplayer"> 
<div class="dlineups-pos">D</div><div><a href="/soccer/player.htm?
id=9368"title="David Mateos">David Mateos</a>...

【问题讨论】:

    标签: python beautifulsoup


    【解决方案1】:

    找到里面的所有a元素和get the text

    home = items.find_all('div', class_="dlineups-half")
    for item in home:
        for a in item.find_all("a"):
            print(a.get_text())
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-29
      相关资源
      最近更新 更多