How to use the urllib module for URL operations in Python 2.x

PHPz
Release: 2023-07-29 11:24:19
Original
1122 people have browsed it

How to use the urllib module for URL operations in Python 2.x

Introduction:
In the Python 2.x version, urllib is a commonly used module for processing network requests, sending requests and Perform operations on URLs. This article will introduce the common usage of the urllib module and give some code examples.

1. Use urllib to send a GET request

Using urllib to send a GET request is very simple, just call the urlopen() function and pass in the URL. The following is a sample code:

import urllib

response = urllib.urlopen('http://www.example.com')   # 发送GET请求

html = response.read()   # 读取响应内容

print(html)   # 打印响应内容
Copy after login

Code analysis:
First, we import the urllib module. Then, use the urlopen() function to send a GET request, where 'http://www.example.com' is passed in as the URL. Next, use the read() method to read the response content and assign the result to the variable html. Finally, use the print statement to print out the response content.

2. Use urllib to send a POST request

Similar to sending a GET request, the method of using urllib to send a POST request is also very simple. The request parameters need to be encoded using the urlencode() function and passed to the urlopen() function through the data parameter. The following is a sample code:

import urllib
import urllib2

values = {'username': 'admin', 'password': '123456'}   # 请求参数

data = urllib.urlencode(values)   # 编码请求参数

url = 'http://www.example.com/login'   # URL

request = urllib2.Request(url, data)   # 创建请求对象

response = urllib2.urlopen(request)   # 发送POST请求

html = response.read()   # 读取响应内容

print(html)   # 打印响应内容
Copy after login

Code analysis:
First, we imported the urllib and urllib2 modules. Then, a dictionary values ​​is created to store the request parameters, including username and password. Next, use the urlencode() function to encode the request parameters, and assign the encoded result to the variable data. Then, assign the URL to the variable url. Next, use the urllib2.Request() function to create a request object request, and pass in the URL and request parameters as parameters. Finally, use the urlopen() function to send the request and read the response content through the read() method.

3. Use urllib for URL parsing

The urllib module provides a urlparse function for parsing URLs. We can use this function to obtain various parts of the URL, such as protocol, domain name, path, etc. The following is a sample code:

import urlparse

url = 'http://www.example.com/login?username=admin&password=123456'   # URL

result = urlparse.urlparse(url)

print(result.scheme)   # 协议

print(result.netloc)   # 域名

print(result.path)   # 路径

print(result.params)   # 参数

print(result.query)   # 查询字符串

print(result.fragment)   # 片段
Copy after login

Code analysis:
First, we imported the urlparse module. Then, assign the URL to the variable url. Next, use the urlparse.urlparse() function to parse the URL and assign the result to the variable result. Then, obtain different parts of the URL through each attribute of the result and print them out respectively.

This article introduces some common methods of using the urllib module to perform URL operations in Python 2.x, and gives corresponding code examples. I hope this article can help everyone better understand and apply the urllib module and improve development efficiency.

The above is the detailed content of How to use the urllib module for URL operations in Python 2.x. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!