8-Bit Borges
8-Bit Borges

Reputation: 10033

BeautifulSoup - extract <a> value

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?

Upvotes: 1

Views: 72

Answers (1)

Bijoy
Bijoy

Reputation: 1131

You should use get_text()

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

would give you u'Dance'

For list of links

all_links = soup.find_all("a")

for link in all_links:
    print link.get_text()

Upvotes: 1

Related Questions