Home > Backend Development > Python Tutorial > How to use urllib.urlopen() function to send GET request in Python 2.x

How to use urllib.urlopen() function to send GET request in Python 2.x

王林
Release: 2023-07-29 08:48:38
Original
1427 people have browsed it

Python is a popular programming language widely used in areas such as web development, data analysis, and automation tasks. In Python 2.x version, you can easily send GET requests and obtain response data using the urlopen() function of the urllib library. This article will introduce in detail how to use the urlopen() function to send a GET request in Python 2.x, and provide corresponding code examples.

Before using the urlopen() function to send a GET request, we first need to import the urllib library, using the following code:

import urllib
Copy after login

Next, we use the urlopen() function to send a GET request. The parameter of the urlopen() function can be a URL address string or a Request object. When sending a GET request, we only need to pass in the URL address. The following is a sample code that uses the urlopen() function to send a GET request and get the response:

response = urllib.urlopen('https://www.example.com')
data = response.read()
print(data)
Copy after login

In the above code, we use the urlopen() function to send a GET request, and the URL address is "https://www .example.com". The urlopen() function returns a file-like object, and we can obtain the response data by calling the read() method. Finally, we print out the obtained data.

It should be noted that the file-like object returned by the urlopen() function needs to be closed manually. In order to better manage resources, we can use the with statement to automatically close the object. The following is a sample code with a with statement:

with urllib.urlopen('https://www.example.com') as response:
    data = response.read()
    print(data)
Copy after login

When using the urlopen() function to send a GET request, we can also simulate the behavior of the browser sending a request by adding request header information. For example, we can add the User-Agent header to pretend to be a different browser. The following is a sample code for adding the User-Agent header:

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
req = urllib.urlopen('https://www.example.com', headers=headers)
data = req.read()
print(data)
Copy after login

In the above code, we define a dictionary type headers variable to store request header information. Then, we create a Request object and pass in the headers as parameters. Finally, we get the response data by calling the read() method.

In summary, the urllib library in Python 2.x provides a convenient urlopen() function, which can be used to send GET requests and obtain response data. When using the urlopen() function, we can simulate the behavior of the browser sending requests by passing in the URL address, adding request header information, etc. By mastering these methods, we can better handle related tasks such as web development, data analysis, and automation tasks. I wish you success in using Python!

The above is the detailed content of How to use urllib.urlopen() function to send GET request 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