How to find HTML elements by attribute using BeautifulSoup?

BeautifulSoup can also be used to scrape elements with custom attributes using the attrs parameter for the functions find and find_all.

To extract elements with the attribute data-microtip-size=medium, the tooltips in the pricing table from ScrapingBee's home page, we can set attrs={"data-microtip-size": "medium"} 

import requests
from bs4 import BeautifulSoup

response = requests.get("https://www.scrapingbee.com")
soup = BeautifulSoup(response.content, 'html.parser')

tooltips = soup.find_all("button", attrs={"data-microtip-size": "medium"})
for tooltip in tooltips:
  print(tooltip.get("aria-label"))
# Output: API credits are valid for one month, leftovers are not rolled-over to the next month... credits and concurrency.

 

Related BeautifulSoup web scraping questions: