Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeautifulSoup - extracting attribute values

If Beautiful Soup gives me an anchor tag like this:

<a class="blah blah" id="blah blah" href="link.html"></a>

How would I retrieve the value of the href attribute?

like image 446
db90 Avatar asked Nov 05 '09 22:11

db90


People also ask

Which method in BeautifulSoup is used for extracting the attributes from HTML?

How do you get attribute value in BeautifulSoup? To extract attributes of elements in Beautiful Soup, use the [~] notation. For instance, el[“id”] retrieves the value of the id attribute.


2 Answers

If you already have the anchor, grab the href attribute like this:

href = anchor["href"]
like image 184
Andrew Hare Avatar answered Sep 24 '22 12:09

Andrew Hare


link.get('href')

in which 'link' is the name of your 'a' tag

like image 24
Sahba E Avatar answered Sep 25 '22 12:09

Sahba E