O'Reilly logo

Python Requests Essentials by Bala Subrahmanyam Varanasi, Rakesh Vidya Chandra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Requests versus urllib2

Let's compare urllib2 and Requests; urllib2.urlopen(), which can be used to open a URL (which can be a string or a request object), but there are many other things that can be a burden while interacting with the web. At this point, a simple HTTP library which has the capabilities to make interaction with the web smooth is the need of the hour, and Requests is one of its kind.

The following is an example for fetching the data from a web service with urllib2 and Requests gives us a clear picture of how easy it is to work with Requests:

The following code gives an example of urllib2:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
 
import urllib2
 
gh_url = 'https://api.github.com'
 
req = urllib2.Request(gh_url)
 
password_manager ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required