Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beautiful Soup find first <a> whose title attribute equal a certain string

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".

  • If I use 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!

like image 555
dj1121 Avatar asked Jul 25 '17 15:07

dj1121


People also ask

What is Find () method in BeautifulSoup?

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.

How do you get attributes of an element in BeautifulSoup?

To extract attributes of elements in Beautiful Soup, use the [~] notation. For instance, el["id"] retrieves the value of the id attribute.

What is Attrs in BeautifulSoup?

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.


1 Answers

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
like image 80
cs95 Avatar answered Sep 29 '22 16:09

cs95