下面是自己写的简单测试代码,仅供参考,api接口就不提供了
import requests
import json
def proxy_api_get():
#请求url,api接口
targetUrl = "******"
resp = requests.get(targetUrl)
if resp.status_code == 200:
b = json.loads(resp.text)
data = b['msg']
for proxy in data:
proxy = "%s:%s"%(proxy['ip'],proxy['port'])
proxies = {
'http': 'http://' + proxy,
'https': 'https://' + proxy,
}
url = 'http://www.baidu.com/'
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",
}
api_test = requests.get(url, headers=headers, proxies=proxies)
if resp.status_code == 200:
return proxies
else:
print('该代理已经失效')
proxy_api_get()
else:
print('请求出错')