【问题标题】:How can I extract the text between <a></a>? [closed]如何提取 <a></a> 之间的文本? [关闭]
【发布时间】:2012-10-25 08:58:23
【问题描述】:

我正在使用 Beautiful Soup,但不知道该怎么做。

</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   

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

【问题讨论】:

  • 看看你用beautifulsoup做了什么
  • from urllib import urlopen from bs4 import BeautifulSoup import re pages = urlopen('last.fm/user/natefanaro/… 然后我不知道还能做什么。
  • @muchacho:这不符合使用 BeautifulSoup的条件。
  • 好吧,老实说我不知道​​自己在做什么。

标签: python tags beautifulsoup


【解决方案1】:
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()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-10
  • 1970-01-01
  • 2015-09-17
  • 2016-02-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多