我想按以下格式提取所有值,例如“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/