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

Viewed 10565

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!

2 Answers

following the same topic in the html file I would like to find just the patent number, title of the citations from the HTML tag. I tried this but it prints all the titles in the HTML file, but I specifically want it under the citations only.

url = 'https://patents.google.com/patent/EP1208209A1/en?oq=medicinal+chemistry'
patent = html_file.read() 
#print(patent)
soup = BeautifulSoup(patent, 'html.parser')
x=soup.select('tr[itemprop="backwardReferences"]')
 y=soup.select('td[itemprop="title"]')
print(y)```
Related