在Python的requests
库中使用代理IP,您需要首先安装requests
和requests-proxy
库
pip install requests requests-proxy
安装完成后,您可以通过以下方法在requests
中使用代理IP:
import os
import requests
proxies = {
'http': os.environ['HTTP_PROXY'],
'https': os.environ['HTTPS_PROXY']
}
response = requests.get('https://www.example.com', proxies=proxies)
print(response.text)
requests-proxy
库设置代理:from requests_proxy import ProxyManager
import requests
proxies = ProxyManager('http://proxy.example.com:8080')
url = 'https://www.example.com'
response = requests.get(url, proxies=proxies)
print(response.text)
import requests
proxies = {
'http': 'http://proxy.example.com:8080',
'https': 'http://proxy.example.com:8080'
}
response = requests.get('https://www.example.com', proxies=proxies)
print(response.text)
请注意,您需要将proxy.example.com:8080
替换为您要使用的实际代理服务器地址和端口。