python - BeautifulSoup - 提取 <a> 值

标签 python beautifulsoup

我想按以下格式提取所有值,例如“Dance”:

 <a href="http://earmilk.com/category/dance/" 
     class="tiptipBlog genre-dance" 
     title="View all posts in Dance" 
     rel="tag">Dance</a>

我已经尝试过:

for a in soup.find_all('a', rel=True):
    tag = a["rel"]

部分有效,打印[u'tag']。但如果我继续:

    print [t.string for t in tag]

我收到以下错误:

AttributeError:“unicode”对象没有属性“string”

我该如何解决这个问题?

最佳答案

您应该使用get_text()

soup.find("a").get_text()

会给你“跳舞”

查看链接列表

all_links = soup.find_all("a")

for link in all_links:
    print link.get_text()

关于python - BeautifulSoup - 提取 <a> 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42451699/

相关文章:

python - 从 python 中的嵌套 URL 中抓取并解析表

python - Webscraping 使用 Python 返回变量值

python - django 'many-to-many' 关系应该驻留在哪一边

python - 使用Scrapy获取整个网站的所有URL

python - 在html中查找dd.mm.yyyy

python - 操作系统的奇怪行为

python - Tensorflow:Word2vec CBOW 模型

python - 如何使用 Beautiful Soup 查找所有具有自定义 html 属性的元素,而不管 html 标签如何?

javascript - 如何使用 beautifulsoup 从 js 和 Reactjs 获取数据?

python - BeautifulSoup Python将输出链接保存到txt文件