How to find element by id 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/")
Universally applicable code would look like this:
r=requests.get("Your URL")
Step 4. Convert the 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:
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)