How to find element by id using Beautifulsoup

A step-by-step guide on how to find elements by id by using Beautifulsoup.

Important: we will use a real-life example in this tutorial, so you will need requests and Beautifulsoup libraries installed.

Step 1. Let’s start by importing the Beautifulsoup library.

from bs4 import BeautifulSoup

Step 2. Then, import requests library.

import requests

Step 3. Get a source code of your target landing page. We will be using our homepage in this example.

r=requests.get("https://proxyway.com/")

A universal code might look like this:

r=requests.get("Your URL")

Step 4. Convert HTML code into a Beautifulsoup object named soup.

soup=BeautifulSoup(r.content,"html.parser")

Step 5. Find an id, which content you would like to extract. We will be using this tag for an example:

finding element id

The code of this id looks like this:

element_by_id=soup.find("section",{"id":"awarded-list"})

Step 6. Let’s check if our code works by printing it out.

print(element_by_id)

Results:
Congratulations, you’ve found and extracted the content of an id using Beautifulsoup. Here’s the full script:

from bs4 import BeautifulSoup
import requests
r=requests.get("https://proxyway.com/")
soup=BeautifulSoup(r.content,"html.parser")
element_by_id=soup.find("section",{"id":"awarded-list"})
print(element_by_id)
Best value provider
Success rate: 98.69%
Response time: 1.29s

Best premium provider
Success rate: 99.57%
Response time: 0.86s

Most customizable provider
Success rate: 99.47%
Response time: 1.08s

Submit a comment

Your email address will not be published. Required fields are marked *

Rate this post