杰瑞科技汇

Python requests如何构造URL请求?

Of course! Here is a comprehensive guide on how to make HTTP requests in Python using the popular requests library.

Python requests如何构造URL请求?-图1
(图片来源网络,侵删)

The Short and Sweet Answer

To get the HTML content of a URL, you first need to install the library and then use a simple function call.

Installation: If you don't have it installed, open your terminal or command prompt and run:

pip install requests

Basic GET Request: This is the most common type of request, used to retrieve data from a server.

import requests
# The URL you want to get data from
url = 'https://www.python.org'
try:
    # Send a GET request to the URL
    response = requests.get(url)
    # Raise an exception if the request was unsuccessful (e.g., 404 Not Found)
    response.raise_for_status()
    # Print the text content of the response
    print(response.text)
except requests.exceptions.RequestException as e:
    # Handle any errors that occur during the request
    print(f"An error occurred: {e}")

Detailed Guide: The requests Library

The requests library is the de facto standard for making HTTP requests in Python due to its simplicity and power. Let's break down its features.

Python requests如何构造URL请求?-图2
(图片来源网络,侵删)

Making Different Types of Requests

The requests library has a function for each HTTP method.

Method Function Description
GET requests.get() Retrieve data from a specified resource.
POST requests.post() Send data to a server to create a resource.
PUT requests.put() Update a resource with the data you send.
DELETE requests.delete() Delete a specified resource.
HEAD requests.head() Similar to GET, but retrieves only the headers.
OPTIONS requests.options() Describe the communication options for the target.

Example of a POST request:

import requests
# The URL for the API endpoint
url = 'https://httpbin.org/post'
# The data you want to send (as a dictionary)
payload = {'key': 'value', 'username': 'testuser'}
# Send the POST request
response = requests.post(url, data=payload)
# Print the JSON response from the server
print(response.json())

httpbin.org is a fantastic testing service that echoes back the request you made, which is perfect for learning.

Handling the Response Object

When you make a request, the server sends back a Response object. This object contains a lot of useful information.

Python requests如何构造URL请求?-图3
(图片来源网络,侵删)
import requests
response = requests.get('https://api.github.com')
# --- Accessing the Response ---
# 1. Status Code
# An HTTP status code indicates whether a specific HTTP request has been successfully completed.
# 200 OK, 404 Not Found, 500 Internal Server Error, etc.
print(f"Status Code: {response.status_code}")
# 2. Headers
# The headers are like metadata for the response.
print("\nResponse Headers:")
print(response.headers)
# 3. Content
# You can get the body of the response in different formats.
# a) As text (good for HTML, plain text)
# print(response.text)
# b) As JSON (good for APIs)
# The .json() method parses the response content and returns it as a Python dictionary or list.
# It will raise an error if the response is not valid JSON.
try:
    data = response.json()
    print("\nJSON Response:")
    print(data)
except requests.exceptions.JSONDecodeError:
    print("\nResponse was not valid JSON.")
# 4. Raw Content
# Access the raw response content (bytes).
# print(response.content)

Passing URL Parameters

Often, you need to pass parameters in the URL, like ?key1=value1&key2=value2. You can do this easily with the params argument.

import requests
# The base URL
url = 'https://httpbin.org/get'
# The parameters you want to pass
# The library will automatically URL-encode these for you
params = {
    'q': 'python requests',
    'page': 2,
    'sort': 'desc'
}
# Send the GET request with parameters
response = requests.get(url, params=params)
# Print the full URL that was sent (including parameters)
print(f"Requested URL: {response.url}")
# Print the response
print(response.json())

Setting Custom Headers

Some websites or APIs require specific headers, such as User-Agent or an Authorization token.

import requests
url = 'https://httpbin.org/headers'
# Define custom headers
headers = {
    'User-Agent': 'My Cool App 1.0',
    'Accept': 'application/json'
}
response = requests.get(url, headers=headers)
print(response.json())

Handling Timeouts and Errors

A network request can fail for many reasons (no internet, server down, invalid URL). It's crucial to handle these exceptions gracefully.

The raise_for_status() method is a great shortcut. It checks if the status code indicates an error (4xx or 5xx) and raises an HTTPError if it does.

import requests
urls_to_try = [
    'https://www.google.com',        # Should work
    'https://thissitedoesnotexistforsure.com', # Should fail (DNS)
    'https://httpbin.org/status/404' # Should fail (404 Not Found)
]
for url in urls_to_try:
    print(f"\nTrying to access: {url}")
    try:
        # Set a timeout of 5 seconds for the request
        response = requests.get(url, timeout=5)
        # This will raise an HTTPError for bad responses (4xx or 5xx)
        response.raise_for_status()
        print(f"Success! Status code: {response.status_code}")
    except requests.exceptions.HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
    except requests.exceptions.ConnectionError as conn_err:
        print(f"Connection error occurred: {conn_err}")
    except requests.exceptions.Timeout as timeout_err:
        print(f"Timeout error occurred: {timeout_err}")
    except requests.exceptions.RequestException as req_err:
        # This catches any other request-related errors
        print(f"An error occurred: {req_err}")

Summary: Key Takeaways

  1. Install requests: pip install requests
  2. Import it: import requests
  3. Make a request: response = requests.get('your_url')
  4. Check the status: response.status_code (aim for 200).
  5. Get the content:
    • For HTML: response.text
    • For JSON APIs: response.json()
  6. Pass parameters: Use the params dictionary.
  7. Set headers: Use the headers dictionary.
  8. Always handle errors: Use a try...except block with response.raise_for_status() and specific exception types.
分享:
扫描分享到社交APP
上一篇
下一篇