How to get text from DIV using Beautifulsoup

A step-by-step guide on how to extract the content of a div tag using Beautifulsoup.

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

Step 1.  First, import Beautifulsoup library.

from bs4 import BeautifulSoup

Step 2. Then, import requests library.

import requests

Step 3. Get your preferred landing page source code. We will use our homepage in this example.

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

An 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:

The code of this id looks like this:

div_text=soup.find("div",{"class":"intro__small-text"}).get_text()

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

print(div_text)

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")
div_text=soup.find("div",{"class":"intro__small-text"}).get_text()
print(div_text)
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