我需要从 HTML 页面中提取艺术家的名字。这是页面的一个片段:
</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-artist" >Example artist name</a>
我已经试过了,但没有成功。
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')
for link in soup('a'):
print html
我哪里搞砸了?
最佳答案
你可以试试这个:
In [1]: from bs4 import BeautifulSoup
In [2]: s = # Your string here...
In [3]: soup = BeautifulSoup(s)
In [4]: for anchor in soup.find_all('a'):
...: print anchor.text
...:
...:
here lies the text i need
这里,find_all
方法返回一个列表,其中包含所有匹配的 anchor 标签,之后我们可以打印 text
属性以获取标签之间的值。
关于python - 如何提取一些 anchor 标签之间的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13247479/