# -*- coding: utf-8 -*- """python3提取api連接並使用requests庫進行http代理""" import requests import random page_url = "https://dev.kdlapi.com/testproxy" # 要訪問的目標網頁 # API接口,返回格式爲json。快代理提供 api_url = "http://kps.kdlapi.com/api/getkps/?orderid=968347822535999&num=1&pt=1&format=json&sep=1" # API接口返回的ip proxy_ip = requests.get(api_url).json()['data']['proxy_list'] # 用戶名和密碼(私密代理/獨享代理) proxies = { "http": "http://%(proxy)s/" % {'proxy': random.choice(proxy_ip)}, "https": "https://%(proxy)s/" % {'proxy': random.choice(proxy_ip)} } print(proxies) headers = { "Accept-Encoding": "Gzip", # 使用gzip壓縮傳輸數據讓訪問更快 } r = requests.get(page_url, headers=headers) # 發送post請求 # r = requests.post("http://dev.kdlapi.com/testproxy", data={"info": "send post request"}, headers=headers) print(r.status_code) # 獲取Response的返回碼 if r.status_code == 200: r.enconding = "utf-8" # 設置返回內容的編碼 # 獲取頁面內容 print(r.content)
Traceback (most recent call last): File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 516, in urlopen body=body, headers=headers) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 304, in _make_request self._validate_conn(conn) File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 724, in _validate_conn conn.connect() File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 237, in connect ssl_version=resolved_ssl_version) File "/usr/lib/python3/dist-packages/urllib3/util/ssl_.py", line 123, in ssl_wrap_socket return context.wrap_socket(sock, server_hostname=server_hostname) File "/usr/lib/python3.4/ssl.py", line 364, in wrap_socket _context=self) File "/usr/lib/python3.4/ssl.py", line 577, in __init__ self.do_handshake() File "/usr/lib/python3.4/ssl.py", line 804, in do_handshake self._sslobj.do_handshake() ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:600)
requests.packages.urllib3.disable_warnings() 關閉告警信息
verify=False 不驗證certificatepython
# -*- coding: utf-8 -*- """python3提取api連接並使用requests庫進行http代理""" import requests import random requests.packages.urllib3.disable_warnings() # 關閉告警信息 page_url = "https://dev.kdlapi.com/testproxy" # 要訪問的目標網頁 # API接口,返回格式爲json。快代理提供 api_url = "http://kps.kdlapi.com/api/getkps/?orderid=968347822535999&num=1&pt=1&format=json&sep=1" # API接口返回的ip proxy_ip = requests.get(api_url).json()['data']['proxy_list'] # 用戶名和密碼(私密代理/獨享代理) proxies = { "http": "http://%(proxy)s/" % {'proxy': random.choice(proxy_ip)}, "https": "https://%(proxy)s/" % {'proxy': random.choice(proxy_ip)} } print(proxies) headers = { "Accept-Encoding": "Gzip", # 使用gzip壓縮傳輸數據讓訪問更快 } r = requests.get(page_url, headers=headers, verify=False) # 不驗證certificate # 發送post請求 # r = requests.post("http://dev.kdlapi.com/testproxy", data={"info": "send post request"}, headers=headers) print(r.status_code) # 獲取Response的返回碼 if r.status_code == 200: r.enconding = "utf-8" # 設置返回內容的編碼 # 獲取頁面內容 print(r.content) # print(r.text)
運行結果
不過我在win10環境下使用同樣的代碼不會引起ssl錯誤,目前懷疑是系統環境ssl的問題。不過最簡單粗暴的辦法就是不驗證,也是最快的方法。json