BeautifulSoup - 提取<a> value

时间:2017-02-25 03:42:23

标签: python beautifulsoup

I'd like to extract all values like "Dance" in the format below:

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

I've tried:

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

which partially works, printing [u'tag']. but then if I go on:

    print [t.string for t in tag]

I get the following error:

AttributeError: 'unicode' object has no attribute 'string'

how do i fix this?

1 个答案:

答案 0 :(得分:1)

您应该使用get_text()

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

会给你你的舞蹈&#39;

链接列表

all_links = soup.find_all("a")

for link in all_links:
    print link.get_text()