要使用代理,可以使用 requests 模組的 proxies 參數來指定代理伺服器的位址。以下是一個示範:

import requests

# 設定代理伺服器的位址
proxy = {
    'http': 'http://IP:PORT',
    'https': 'http://IP:PORT'
}

# 使用 GET 請求並指定代理伺服器
response = requests.get('https://www.example.com', proxies=proxy)

# 檢查回應狀態碼
if response.status_code == 200:
    print('成功取得回應')
else:
    print('取得回應失敗')

proxies 參數中,http 是指定使用 HTTP 代理,https 則是指定使用 HTTPS 代理。IP:PORT 部分需要替換為實際的代理伺服器的位址和埠號。

如果需要驗證代理伺服器的使用者名稱和密碼,可以在位址後加上 username:password@,例如:http://username:password@IP:PORT

python requests怎麼使用代理

原文地址: http://www.cveoy.top/t/topic/iJOI 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录