How to extract text from a table using Beautifulsoup

A step-by-step guide on how to extract text from the table 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 landing page 5 Methods for Scraping Google Search Results in this example.


An universal code might look like this:

r=requests.get("Your URL")

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


Step 5. Extract text from a table. There are several different ways to accomplish this task. If there is only one table in your HTML document (or you just need the text from the first table), this piece of code might be suitable for you.


However, if you need text from a specific table, there’s another way. Suppose we want to extract the text of the third table.

We will use this code for this task:


Universal code for extracting the table text according to its sequence number in the HTML document looks like this:


In this code, n is the sequence number of the table you selected.

If you know the id or class of a table whose text you want to extract, you can use the following code to extract text from a specific table:
For Classes:

text_table=soup.find("table",{"class":"class you want to extract"}).get_text()

For IDs:

text_table=soup.find("table",{"id":"id you want to extract"}).get_text()

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


print text table

Congratulations, you’ve extracted a text from a table using Beautifulsoup. Here’s the full script:

from bs4 import BeautifulSoup
import requests
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