Python Urllib: The powerful functions and flexible use of the network library (stdlib)

When we talk about network programming in Python, the built-in library Urllib is undoubtedly the one we should focus on. Urllib provides Python developers with a comprehensive and easy-to-use API for handling network requests and data transfer. With Urllib, we can send HTTP requests, perform URL encoding and decoding, handle cookies and sessions, etc. Below, we will take you into the world of Python Urllib and explore the various functions of this powerful network library.

1. First introduction to Urllib: basic concepts and usage

Python’s Urllib library can be used to handle web-related tasks. It can be used to send HTTP requests, encode and decode URLs, handle cookies, and can even be used to implement basic web crawlers. Below, we will start with the basic concepts and usage, and gradually explore the various functions of the Urllib library.

1.1 import urllib library

First, we need to import the Urllib library. You can import it by adding the following line to your code:

import urllib

1.2 Send GET request

The GET request function of the Urllib library allows us to easily obtain the content of the web page. Here is an example of a basic GET request:

import urllib.request
  
url = 'http://example.com'
response = urllib.request.urlopen(url)
content = response.read()
print(content)

This code sends a GET request by creating a URL object and calling the urlopen method. We then use the read method to get the returned content and print it out.

1.3 Send POST request

In addition to GET requests, Urllib can also send POST requests. Here is an example of a basic POST request:

import urllib.request
import urllib.parse
  
url = 'http://example.com'
data = {'key1': 'value1', 'key2': 'value2'}
encoded_data = urllib.parse.urlencode(data)
  
request = urllib.request.Request(url, encoded_data, method='POST')
response = urllib.request.urlopen(request)
content = response.read()
print(content)

This code first creates a URL-encoded string containing the POST data. Then, we create a Request object and set the URL, encoded data, and method to ‘POST’. Finally, we use the urlopen method to send the request and the read method to get the returned content.

1.4 Handling HTTPS requests

The Urllib library also supports handling HTTPS requests. To send an HTTPS request, you simply create an SSL context and pass it to the urlopen method. Here is an example of handling HTTPS requests:

import urllib.request
import ssl
  
context = ssl.create_default_context()
url = 'https://example.com'
response = urllib.request.urlopen(url, context=context)
content = response.read()
print(content)

This code creates an SSL context and uses it to send HTTPS requests. Then, we use the read method to get the returned content.

1.5 Handling URL encoding

The Urllib library also provides tools for handling URL encoding. Here’s an example of encoding and decoding a URL:

import urllib.parse
  
data = {'key1': 'value1', 'key2': 'value2'}
encoded_data = urllib.parse.urlencode(data)
print(encoded_data)
  
decoded_data = urllib.parse.parse_qs(encoded_data)
print(decoded_data)

This code first URL-encodes the dictionary using the urlencode method. We then use the parse_qs method to parse the encoded string into a dictionary.

The above are some basic ways to use the Python Urllib library. This library also has many other functions, such as handling cookies, sessions, HTTP headers, etc. We will explain in detail how to use these advanced functions in subsequent chapters.

example:

When using the urllib library in Python for network programming, you can use the urllib.request module to send HTTP requests and use the urllib.parse module to process URL. Here is a complete example that demonstrates how to use the urllib library to send a GET request and handle URL encoding:

import urllib.request
import urllib.parse
  
#Send GET request
url = 'http://example.com'
response = urllib.request.urlopen(url)
content = response.read()
print(content)
  
#Send a GET request with parameters
params = {'key1': 'value1', 'key2': 'value2'}
encoded_params = urllib.parse.urlencode(params)
url_with_params = f"{url}?{encoded_params}"
response = urllib.request.urlopen(url_with_params)
content = response.read()
print(content)
  
# Handle URL encoding and decoding
data = {'key1': 'value1', 'key2': 'value2'}
encoded_data = urllib.parse.urlencode(data)
print(encoded_data)
decoded_data = urllib.parse.parse_qs(encoded_data)
print(decoded_data)

In the above example, the urllib.request.urlopen() method is first used to send a simple GET request, obtain the content of the web page, and print it out. Then, use the urllib.parse.urlencode() method to URL-encode the parameters and add them to the URL, sending a GET request with the parameters. Finally, URL encoding and decoding are handled using the urllib.parse.urlencode() and urllib.parse.parse_qs() methods.

When using the urllib library in Python to send a POST request, you can use the urlopen() method in the urllib.request module and Request class. Here is an example showing how to send a POST request:

import urllib.request
import urllib.parse
  
# Build POST data
data = {'key1': 'value1', 'key2': 'value2'}
encoded_data = urllib.parse.urlencode(data)
  
#Create Request object
url = 'http://example.com'
request = urllib.request.Request(url, data=encoded_data)
  
#Send POST request
response = urllib.request.urlopen(request)
content = response.read()
print(content)

In the above example, the POST data to be sent is first constructed and encoded into a URL-encoded format using the urllib.parse.urlencode() method. Then, a Request object is created and the URL and encoded data are passed to it as parameters. Finally, use the urllib.request.urlopen() method to send a POST request and read the response content.

Please note that if you want to send JSON data, you need to use the json.dumps() method to serialize the data into JSON format and set the appropriate Content-Type in the request header. Here is an example of sending JSON data:

import urllib.request
import json
  
# Build JSON data
data = {'key1': 'value1', 'key2': 'value2'}
json_data = json.dumps(data)
  
#Create Request object
url = 'http://example.com'
request = urllib.request.Request(url, data=json_data)
request.add_header('Content-Type', 'application/json')
  
#Send POST request
response = urllib.request.urlopen(request)
content = response.read()
print(content)

In this example, the json.dumps() method is used to serialize a Python dictionary into a JSON string. Then, when creating the Request object, pass it the JSON data as a parameter. In addition, use the add_header() method to set the Content-Type of the request header to application/json, which is required when sending JSON data.

Recommended introductory books:

The following are several classic books suitable for getting started with Python, as well as their introductions:

  1. “Python Programming: From Introduction to Practice” (by Eric Matthes) – This is an introductory book for Python beginners. It has detailed content and rich examples, which can help readers quickly master the basics of Python programming.
  2. “Fluent Python” (by Luciano Ramalho) – This book is an introduction to the features of the Python language. It covers Python’s syntax, common libraries, functional programming and other aspects. It is suitable for readers who want to understand Python in depth. .
  3. “Python Core Programming” (by Wesley Chun) – This book is a relatively comprehensive Python programming book. It covers Python’s basic syntax, network programming, web development, GUI programming and other aspects. It is suitable for those who want to have a comprehensive understanding of Python. Reader of Python applications.
  4. “Python Data Analysis Basics Tutorial” (written by Anaconda Education Team) – This book is an introductory book to Python for data analysts. It introduces common data analysis libraries such as Pandas, NumPy, and Matplotlib, and is suitable for those who want to learn Python data. Analytical readers.
  5. “Python Neural Network Programming” (by Tariq Rashid) – This book is an introductory Python book for machine learning enthusiasts. It introduces how to use Python to build neural networks and is suitable for readers who want to learn Python machine learning.
  6. “Python Gray Hat: Hacking and Penetration Testing Programming” (by Justin Seitz) – This book is an introductory book to Python for security enthusiasts. It introduces how to use Python for penetration testing and is suitable for those who want to learn Python security. Programming Reader.
  7. “Python Natural Language Processing” (by Bird, Klein, Loper) – This book is an introductory book on Python for natural language processing enthusiasts. It introduces how to use Python for natural language processing such as text analysis, word segmentation, and part-of-speech tagging. Task, suitable for readers who want to learn Python natural language processing.
  8. “Python Algorithm Tutorial” (by David M. Beazley) – This book is an introductory Python book for algorithm enthusiasts. It introduces how to use Python to implement various algorithms and is suitable for readers who want to learn Python algorithm programming.
  9. “Getting Started with Python Game Programming Quickly” (written by the Pygame Development Team) – This book is an introductory Python book for game development enthusiasts. It introduces how to use the Pygame library to develop games and is suitable for readers who want to learn Python game development.
  10. “Python Crawler Development and Practical Combat” (authored by Li Junming) – This book is an introductory Python book for crawler development enthusiasts. It introduces how to use Python to develop web crawlers and is suitable for readers who want to learn Python web crawlers.

The knowledge points of the article match the official knowledge files, and you can further learn relevant knowledge. Python entry skill treeWeb crawlerurllib361200 people are learning the system