【问题标题】:Beautifulsoup text from tags inside of tags来自标签内标签的 Beautifulsoup 文本
【发布时间】:2018-09-24 12:06:54
【问题描述】:

我正在尝试获得Artist resultsSong results,但是我不确定如何获得我想要的这两个值。这就是我所拥有的:

albums = soup.find('div', attrs={'class':'panel-heading'}, text=re.compile('Artist results:'))
        for i in albums.find_all('a'):
            print (i)

我想从artist results 得到什么:asap rocky、asap mob、asap ferg

同样,我想从song results 获得的信息是:ASAP by T.I、ASAP by Eric Bellinger 等。

<div class="panel">
    <div class="panel-heading"><b>Artist results:</b><br><small>[1-3 of 3 total <span class="text-lowercase">Artists</span> found]</small></div>
    <table class="table table-condensed">
        <tr><td class="text-left visitedlyr">
            1. <a href="https://www.azlyrics.com/a/asaprocky.html" target="_blank"><b>Asap Rocky</b></a></td>
        </tr>
        <tr><td class="text-left visitedlyr">
            2. <a href="https://www.azlyrics.com/a/asapmob.html" target="_blank"><b>Asap Mob</b></a></td>
        </tr>
        <tr><td class="text-left visitedlyr">
            3. <a href="https://www.azlyrics.com/a/asapferg.html" target="_blank"><b>Asap Ferg</b></a></td>
        </tr>
    </table>
</div>
<div class="panel">
    <div class="panel-heading"><b>Song results:</b><br><small>[1-5 of 454 total <span class="text-lowercase">Songs</span> found]</small></div>
    <table class="table table-condensed">
        <tr><td class="text-left visitedlyr">
            1. <a href="https://www.azlyrics.com/lyrics/ti/asap.html" target="_blank"><b>ASAP</b></a>  by <b>T.I.</b><br>
            <small>[Intro] <strong>Asap</strong>, <strong>asap</strong>, <strong>asap</strong> <strong>Asap</strong>, <strong>asap</strong>, <strong>asap</strong> Ay, ay, ay, ay, ay, you niggaz better exit <strong>Asap</strong>, <strong>asap</strong>, <strong>asap</strong>, <strong>asap</strong> Ay-s, ay-p, ay-s, ay-p <strong>Asap</strong>, <strong>asap</strong>, <strong>asap</strong>, <strong>asap</strong> Ay-s, ay-p, ay-s, ay-p <strong>Asap</strong>, <strong>asap</strong>, <strong>asap</strong>, <strong>asap</strong> A-s-a-p, A-S-A-P [Verse 1] I'm on my grind, grand h...</small></td>
        </tr>
        <tr><td class="text-left visitedlyr">
            2. <a href="https://www.azlyrics.com/lyrics/ericbellinger/asap.html" target="_blank"><b>ASAP</b></a>  by <b>Eric Bellinger</b><br>
            <small>ou say? Cause girl I need that <strong>asap</strong> [Hook] Girl I need that <strong>asap</strong>, <strong>asap</strong>, <strong>asap</strong>, I need Girl I need that <strong>asap</strong>, <strong>asap</strong>, <strong>asap</strong>, I need Girl I need that <strong>asap</strong>, <strong>asap</strong>, <strong>asap</strong>, baby I need Girl I'm tryina taste that, taste that, I need Girl I need that <strong>asap</strong>, <strong>asap</strong>, ...</small></td>
        </tr>

【问题讨论】:

    标签: python regex web-scraping beautifulsoup


    【解决方案1】:

    获得 Artists 的结果非常简单。首先,使用soup.find('b', text='Artist results:') 找到&lt;b&gt;Artist results:&lt;/b&gt; 标签。然后使用find_next('table')找到有结果的表。

    artists_table = soup.find('b', text='Artist results:').find_next('table')
    artists = [x.text for x in artists_table.find_all('a')]
    print(artists)
    # ['Asap Rocky', 'Asap Mob', 'Asap Ferg']
    

    要获取歌曲结果,请使用相同的方法获取表格。但是,要获得您想要的文本,您必须进行一些更改。

    songs_table = soup.find('b', text='Song results:').find_next('table')
    songs = [' by '.join(b.text for b in td.find_all('b')) for td in songs_table.find_all('td')]
    print(songs)
    # ['ASAP by T.I.', 'ASAP by Eric Bellinger']
    

    【讨论】:

      【解决方案2】:

      据我所知,BeautifulSoup 仅适用于标签。

      这是你想要达到的目标

      elements = bs.find_all('div',attrs={"class":"panel"})
      for i in elements:
          if i.div.b.text == "Artist results:":
              artist_a_tag = i.find_all("a")
          if i.div.b.text == "Song results:":
              songs_a_tag = i
      artist_results = [i.b.text for i in artist_a_tag]
      songs_with_artist = re.findall(r"target=\"_blank\"><b>(.*?)<\/b><\/a>\s+by\s+<b>(.*?)<\/b><br", str(songs_a_tag), re.M|re.I|re.S)
      songs_results = [" by ".join(i) for i in songs_with_artist]
      

      希望这能解决您的问题,如果没有,请告诉我

      【讨论】:

      • 这行得通,但是,在解析 HTML 时最好避免使用正则表达式。或者,尽量使用它。
      • 如果我们只想使用 BeautifulSoup,那么代码会变得有点长。为了避免这种情况,我融合了这两种方法。
      • 其实对于这个问题,代码不会变大(看看我的解决方案)。此外,使用正则表达式获得通用解决方案更加困难。有很多因素需要考虑。
      • 是的,我看到了。你已经给出了最好的解决方案。我的脑海中跳过了表格方法@KeyurPotdar
      猜你喜欢
      • 2015-11-04
      • 2016-10-17
      • 1970-01-01
      • 2020-02-26
      • 2014-10-02
      • 1970-01-01
      • 2016-01-10
      相关资源
      最近更新 更多