如何在<a></a>之间提取文字?

时间:2012-11-05 13:54:53

标签: python tags beautifulsoup

我使用的是美丽的汤,但无法弄清楚如何做到这一点。

</td>
        <td class="playbuttonCell">
        <a class="playbutton preview-track" href="/music/example" data-analytics-redirect="false"  ><img class="transparent_png play_icon" width="13" height="13" alt="Play" src="http://cdn.last.fm/flatness/preview/play_indicator.png" style="" /></a>    </td>
                                                        <td class="subjectCell" title="example, played 3 times">
            <div>
                                        <a href="/music/example"   >here lies the text i need</a>

这不是在做这项工作

print soup('a')

for link in soup('a'):
    print html   

打印所有内容,我还能尝试什么?

1 个答案:

答案 0 :(得分:2)

import urllib
from bs4 import BeautifulSoup

html = urllib.urlopen('http://www.last.fm/user/Jehl/charts?rangetype=overall&subtype=artists').read()
soup = BeautifulSoup(html)
print soup('a')
# prints [<a href="/" id="lastfmLogo">Last.fm</a>, <a class="nav-link" href="/music">Music</a>....

获取每一个的文本。

for link in soup('a'):
    print link.get_text()