5 Lib to Grab Data Out of a Web Page in Python

November 18, 2021
5 Lib to Grab Data Out of a Web Page in Python

Python is one of the most popular web technologies nowadays that provides a variety of libraries to scrape the web, such as Scrapy, BeautifulSoup, Requests, Urllib, and Selenium. And that’s not all, for sure. Python is growing very fast, so we are expecting more and more new libraries and top-notch tools for harvesting data. In this article, we will give an overview of the most popular libraries for web scraping, so grab yourself a cup of coffee and learn how to web scrape with Python.

Python Web Scraping Library: How It Works

Let’s start with the definition of web scraping Python. Web scraping is an automatic gathering of public data from any website with the help of web scrapers. Web scrapers automatically pull out large amounts of public data in seconds to use the vast amount of publicly available web data to make smarter decisions.

The main benefit of web scraping is that it provides structured web data from any public website. The web scraping process can not exist without two elements: the crawler and the scraper.

A web crawler or a spider is an artificial intelligence that explores the data by following different links on the website when a web scraper quickly extracts this data from a web page.

This is how the usual web scraping process looks like:

  • Identify the target website
  • Collect URLs of the pages you want to extract data from
  • Make a request to these URLs to get the HTML of the page
  • Use locators to find the data in the HTML
  • Save the data in a JSON or CSV file, or some other structured format

Web scraping is used for different purposes such as market research, competitor monitoring, research, and development, news and content monitoring, etc.

How do libraries help in this case? With the help of Python libraries, you can ensure that this process is conducted without any errors, as you can handle multiple data crawling or web scraping without any complicated code or cumbersome processes. Let’s check out 5 libs to grab data out of a web page in Python.

Requests Library

Requests is a super simple Python web scraper library for web scraping used to make various types of HTTP requests like GET, POST, etc. This library is very simple to use and is considered the most crucial library for web scraping. However, keep in mind that this library does not parse the retrieved HTML data, and you have to use other libraries such as BeautifulSoup and lxml for this purpose.

Here are the main advantages and disadvantages of the Requests Python web scraping libraries.


  • Simple
  • Basic/Digest Authentication
  • International Domains and URLs
  • Chunked Requests
  • HTTP(S) Proxy Support


  • Retrieves only static content of a page
  • Can’t be used for parsing HTML
  • Can’t handle websites made purely with JavaScript

So when should you use scrapy vs beautifulsoup?

Use it if you’re just starting with web scraping and don’t have much experience in this field. Also, make sure that you have an API to contact with. Requests have very detailed and structured documentation, and it supports the full restful API with all methods.

It’s better to avoid using Requests if the web page you want to grab data from has JavaScript content. In this case, the responses may parse incorrect information.

Python Requests Code Example

Let’s take a look at the examples of Python requests code stated in the Geeksforgeeks tutorial. When one makes a request to a URI, it returns a response.  This Response object in terms of Python is returned by requests.method(), method being – get, post, put, etc. For example, response.status_code returns the status code from the headers itself, and one can check if the request was processed successfully or not. Here’s an example:

BeautifulSoup 4 Library

BeautifulSoup is the most widely-used Python web scraper library. It builds a parse tree for parsing HTML and XML documents and automatically converts incoming documents to Unicode and outgoing documents to UTF-8. You can also combine this library with other parses life lxml to make it perform faster.

One of the most distinct benefits of the BeautifulSoup library is that it works well with HTML, even if it’s poorly designed and has many features.


  • Requires a few lines of code
  • Great documentation
  • Easy to learn for beginners
  • Robust
  • Automatic encoding detection


  • Slower than lxml, but instead of looking for beautifulsoup alternatives, you can swap out its parser with a faster one if you need the speed.

BS4 also helps you to navigate a parsed document and find what you need for building common applications. For example, you need to type only a few lines to find all the links on the web page.

However, keep in mind that this library is only designed for parsing and cannot request data from web servers in the form of HTML documents/files and still requires a parser.

“Web scraping is great and can save you plenty of time when you want to quickly extract data from websites. The examples above are used for you to quickly get started. Of course, there’s more to it than what I showed above e.g. (crawling, pagination, viewing the DOM, authentication, cookies, etc.). This is only the tip of the iceberg.”

Martin Breuss, Lead Python Programming Instructor at Real Python – Online Python Training & Expert Community.

Python BeautifulSoup 4 Code Example

Here is an example of scraping a web page using Beautiful Soup 4. First of all, you need to import the necessary libraries. Then, use function “prettify” to look at the nested structure of the HTML page. To extract all the links within <a>, we will use “find_all(). You’ll receive the following output:

Image by Proxet, Custom Software Development Company. Scraping a Web Page Using BeautifulSoup
Scraping a Web Page Using BeautifulSoup

BeautifulSoup allows to perform different types of web scraping to collect data from web pages and eliminate manual work. To scrap the web pages more effectively, you can also use attributes like .parent, .contents, .descendants and .next_sibling, .prev_sibling and various attributes to navigate using tag name.

Selenium Library

As you can see, if the website is written in JavaScript, it is hard for Python libraries to extract data from static web pages, and most of the libraries do not work with dynamic data. That’s where Selenium comes for help.

Selenium is an open-source browser automation tool (web driver) that allows you to automate processes such as logging into a social media platform. It’s widely used for test cases or test scripts on web applications. It can render web pages by running JavaScript, which is not common for other Python libraries.

To start using the Selenium python scraping library, you’ll need three components:

  • Web Browser – supported browsers are Chrome, Edge, Firefox, and Safari
  • Driver for the browser – see this page for links to the drivers
  • The selenium package
  • The selenium package can be installed from the terminal
“We use Selenium daily in my organization. We started using it to run basic automation testing of our web forms. This helped us significantly cut down on the time that it was taking to submit lead tests manually. This ended up leading to full automation of entire test suites.”

Janice Cruz, QA at Deltak – an education management platform.

Selenium can mimic human behavior, as it harvests any dynamic content and makes it available for parsing by built-in methods or even BeautifulSoup. However, the Selenium has one slight disadvantage – as it should execute the JavaScript code first before parsing, that’s why it may affect the general speed. But if the lack of speed is not a drawback for you and you wish to extract data at a lower scale, Selenium is a great way to go.

Python Selenium Code Example

Here’s an example of the code for installing Selenium Python and Python web parser. Here’s what you should do to start the first test in Selenium with the Python.

  • Import the webdriver and Keys classes from Selenium
  • Create an instance of Chrome with the path of the driver that you downloaded through the websites of the respective browser
  • Use the .get() method of the driver to load a website
  • After selecting the DOM element, clear its contents using the .clear() method, enter a string as its value using the .send_keys() method and finally, emulate the press of the Return key using Keys.RETURN.

Scrapy Python Library

Scrapy is more than just a library. It’s a web scraping framework that provides spider bots for crawling multiple websites and extracting the data. It allows you to create your spider bots, host them on Scrapy Hub, or as an API. With Scrapy, creating fully functional spiders is easy and doesn’t take a lot of time.

Scrapy is asynchronous, so it can perform multiple HTTP requests simultaneously. It saves a lot of time and increases the efficiency of our workflow. You can also add plugins to Scrapy to enhance its functionality and pair it with a library called Splash, a lightweight web browser. Extracting data from dynamic websites is also possible.


  • Asynchronous
  • Excellent documentation
  • Various plugins
  • Create custom pipelines and middlewares
  • Low CPU and memory usage
  • Well designed architecture
  • A plethora of available online resources


  • Steep learning curve
  • Overkill for easy jobs
  • Not beginner-friendly
“Python is, it’s easily one of the most common and practical programming languages for web scraping. With the right library, you could build a Web Scraper in no time, which is why you should definitely consider the libraries we discuss in this blog post.”

George Serebrennikov, COO at Proxet (ex – Rails Reactor) – a custom software development solutions company.

Python Scrapy Code Example

Here’s an example of extracting data from the eCommerce website with Scrapy. The attribute “data-img” of the <img> tag can be used to extract image URLs. In order to use the images pipeline to download images, it needs to be enabled in the settings.py file. Add the following lines to the file. Here’s an example of web data scraping from Reddit with the help of Scrapy.

Image by Proxet, Custom Software Development Company. Exporting Scraped Data as a CSV
Exporting Scraped Data as a CSV

Lxml Python Library Overview

lxml is a Python library for handling XML and HTML files, as well as for web scraping. There are a lot of off-the-shelf XML parsers out there, but for better results, developers sometimes prefer to write their own XML and HTML parsers. This library is extremely fast when parsing large documents, very well documented, and provides easy conversion of data to Python data types, resulting in easier file manipulation.

In comparison with other libraries, the python lxml package has two advantages that make it outstanding:

  • Performance: Reading and writing even fairly large XML files take an imperceptible amount of time.
  • Ease of programming: python lxml library has an easy syntax and more adaptive nature than other packages.

You can use lxml library for creating XML/HTML structure using elements or parsing XML/HTML structure for retrieving information. With the help of this library, you can get information from different web services and web resources, as these are implemented in XML/HTML format. If you want to learn how to get started with lxml, check out this tutorial.

With the Python programming language, you have many options to scrap data. Regardless of the tools you use, decide first on the main task that the library should solve, and then determine which library is most suitable for your job.

The best solution is not always the most complex and popular tool. Sometimes, you can use a simpler tool that uses less code and takes much less time. Proxet has a wealth of developer and data expert experience to help you come up with the best library for data parsing. Contact us, and let’s choose the best solution for your project together.

Related Posts