Last modified: Jan 10, 2023 By Alexander Williams

How to Find by ID and Class in BeautifulSoup

To find by ID and class, we can use:

  • ID and class_ parameters
  • attrs parameter
  • CSS selector

Using ID and class_ parameters

In the following example, well find <p> tag that has "bs" in the ID value and "p" in the class value.

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
hrml_source = '''
<p class="p">Hello Python</p>
<p class="p" id="bs">Hello BeautifulSoup</p>
'''

soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing

p = soup.find(id="bs", class_="p") # 👉️ Find By ID and Class

print(p) # 👉️ Print Result

Output:

<p class="p" id="bs">Hello BeautifulSoup</p>

Note: This method also works with the find_all() function.

Using attrs parameter

Another way to find by ID and class is using the attrs parameter. In the following example, we'll do the same as the previous example.

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
hrml_source = '''
<p class="p">Hello Python</p>
<p class="p" id="bs">Hello BeautifulSoup</p>
'''

soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing

p = soup.find(attrs={"id":"bs", "class":"p"}) # 👉️ Find By ID and Class

print(p) # 👉️ Print Result

Output:

<p class="p" id="bs">Hello BeautifulSoup</p>

Using CSS selector

To find by CSS selector, we need to use select() or select_one() function. However, we'll see how to find by ID and Class using CSS selector in the following example.

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
hrml_source = '''
<p class="p">Hello Python</p>
<p class="p" id="bs">Hello BeautifulSoup</p>
'''

soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing

p = soup.select_one("#bs.p") # 👉️ Select By ID and Class

print(p) # 👉️ Print Result

#bs.p: is a CSS selector means tag with bs in the ID value and p in the class value.

Output:

<p class="p" id="bs">Hello BeautifulSoup</p>

Also, you can use a selector with ID or Class parameter.

from bs4 import BeautifulSoup # 👉️ Import BeautifulSoup module

# 👇 HTML Source
hrml_source = '''
<p class="p">Hello Python</p>
<p class="p" id="bs">Hello BeautifulSoup</p>
'''

soup = BeautifulSoup(hrml_source, 'html.parser') # 👉️ Parsing

p = soup.select_one("#bs", class_="p") # 👉️ Select By ID and Class

print(p) # 👉️ Print Result

Output:

<p class="p" id="bs">Hello BeautifulSoup</p>