在Python中使用requests模块挂载代理非常简单。可以通过在请求中传递一个proxies参数来实现。这是一个包含代理设置的字典。以下是一个具体的例子:

import requests

# 代理设置
proxies = {
    "http": "http://10.10.1.10:3128",
    "https": "https://10.10.1.10:1080",
}

# 发送GET请求
response = requests.get("http://example.com", proxies=proxies)

# 输出响应内容
print(response.text)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

在这个例子中,proxies字典指定了HTTP和HTTPS请求的代理服务器地址。如果你需要认证代理服务器(即代理服务器需要用户名和密码),可以按如下方式设置:

import requests

# 带有认证的代理设置
proxies = {
    "http": "http://user:password@10.10.1.10:3128",
    "https": "https://user:password@10.10.1.10:1080",
}

# 发送GET请求
response = requests.get("http://example.com", proxies=proxies)

# 输出响应内容
print(response.text)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

在这个例子中,userpassword是你的代理服务器的用户名和密码。

此外,如果你只需要设置HTTP或HTTPS代理,可以单独设置:

import requests

# 只设置HTTP代理
proxies = {
    "http": "http://10.10.1.10:3128",
}

# 发送GET请求
response = requests.get("http://example.com", proxies=proxies)

# 输出响应内容
print(response.text)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

或者:

import requests

# 只设置HTTPS代理
proxies = {
    "https": "https://10.10.1.10:1080",
}

# 发送GET请求
response = requests.get("https://example.com", proxies=proxies)

# 输出响应内容
print(response.text)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

通过这种方式,你可以方便地在Python中使用requests模块挂载代理服务器。