python - 如何提取一些 anchor 标签之间的文本?

标签 python anchor beautifulsoup scraper

我需要从 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/

相关文章:

python - 计算 PySpark 列中的列值

python - 如何根据另一列添加到 Pandas 列

javascript - anchor 的外部链接 - 可以延迟滚动吗?

selenium - Requests 或 Urllib - 登录网站,将下载请求发送到 url,并另存为 xlsx

python - 如何伪造 Python 请求/beautifulsoup 中启用的 javascript

python - flask 重定向 "XMLHttpRequest cannot load..."错误本地主机

python - 想要在匹配的字符串中添加遗漏的字符串

php - 使用 PHP/MySQL 创建动态链接

Jquery "Live"点击事件未在 anchor 标记内触发

Python BeautifulSoup 错误