How to use CSS Selectors in Python?

There are multiple ways to use CSS Selectors in Python. The method you choose will depend on the library you are using. Some of the most famous libraries that allow using CSS Selectors are BeautifulSoup and Selenium.

You can use the select or select_all methods of BeautifulSoup and pass in a CSS selector as an argument. This is how it will look like:

import re
import requests
from bs4 import BeautifulSoup

html = requests.get("https://scrapingbee.com").text
soup = BeautifulSoup(html)

print(soup.select("h1"))
# Output: <h1 class="mb-33">Tired of getting blocked while scraping the web?</h1>

print(soup.select("h1.mb-33"))
# Output: <h1 class="mb-33">Tired of getting blocked while scraping the web?</h1>

Alternatively, you can use CSS selectors in Selenium to do the same thing. Here is some sample code:

from selenium import webdriver
from selenium.webdriver.common.by import By

DRIVER_PATH = '/path/to/chromedriver'
driver = webdriver.Chrome(executable_path=DRIVER_PATH)

# Open Scrapingbee's website
driver.get("http://www.scrapingbee.com")

# Get the first h1 element using find_element
h1 = driver.find_element(By.CSS_SELECTOR, "h1")

print(h1.text)
# Output: 'Tired of getting blocked while scraping the web?'

# Get the first h1 element with the class of mb-33
h1 = driver.find_element(By.CSS_SELECTOR, "h1.mb-33")

print(h1.text)
# Output: 'Tired of getting blocked while scraping the web?'

Related CSS Selectors web scraping questions: