Find All Instances of a Tag with Beautiful Soup

Python

To find all instances of a tag on webpage with Beautiful Soup, we use the find_all function and pass the tag that we want to search for. The function returns a list with all occurrences of the tag in question.

In the example below we are looking for all instances of the h2 tag within a Wikipedia webpage.

 1|  import requests
 2|  from bs4 import BeautifulSoup
 3|  
 4|  response = requests.get('https://en.wikipedia.org/wiki/FTSE_100_Index')
 5|  parser = BeautifulSoup(response.content, 'html.parser')
 6|  
 7|  h2 = parser.find_all('h2')
Did you find this snippet useful?

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