Find the Nth Tag On a WebPage with Beautiful Soup


In this example we get the first h2 tag from a wikipedia page using Beautiful Soup. To do this we first find all the h2 tags as a list called h2_tags and then simply get the tags in the first position of the list.

 1|  import requests
 2|  from bs4 import BeautifulSoup
 4|  response = requests.get('')
 5|  parser = BeautifulSoup(response.content, 'html.parser')
 7|  h2_tags = parser.find_all('h2')
 9|  # get the first h2 tag
10|  print(h2_tags[0])
Did you find this snippet useful?

Sign up for free to to add this to your code library