I'm working with beautiful soup and am trying to grab the first tag on a page that has the attribute equal to a certain string.
For example:
<a href="url" title="export"></a>
What I've been trying to do is grab the href of the first that is found whose title is "export".
soup.select("a[title='export']")
then I end up finding all tags who satisfy this requirement, not just the first.If I use find("a", {"title":"export"})
with conditions being set such that the title should equal "export", then it grabs the actual items inside the tag, not the href.
If I write .get("href")
after calling find()
, I get None back.
I've been searching the documentation and stack overflow for an answer but have yet found one. Does anyone know a solution to this? Thank you!
find() method The find method is used for finding out the first tag with the specified name or id and returning an object of type bs4. Example: For instance, consider this simple HTML webpage having different paragraph tags.
To extract attributes of elements in Beautiful Soup, use the [~] notation. For instance, el["id"] retrieves the value of the id attribute.
contents attribute of a BeautifulSoup object is a list with all its children elements. If the current element does not contain nested HTML elements, then . contents[0] will be just the text inside it.
What I've been trying to do is grab the href of the first that is found whose title is "export".
You're almost there. All you need to do is, once you've obtained the tag, you'll need to just index it to get the href. Here's a slightly more bulletproof version:
try:
url = soup.find('a', {'title' : 'export'})['href']
print(url)
except TypeError:
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With