hikyuu2/hikyuu/fetcher/proxy/proxy.py

39 lines
1.1 KiB
C++
Raw Normal View History

2020-12-05 17:53:38 +08:00
# coding:utf-8
#
# The MIT License (MIT)
#
# Created on: 2020-11-29
# Author: fasiondog
import requests
import json
import datetime
2022-12-16 14:45:52 +08:00
import time
2020-12-07 00:27:55 +08:00
from hikyuu.util import hku_warn, hku_info, hku_check
2020-12-05 17:53:38 +08:00
from .zhima import get_proxy
def request_with_proxy(url):
"""通过代理进行请求,访问失败将抛出异常"""
# 获取到的 ip 可能无法访问相应的 url重试10次以便找到能用的 proxy
new = False
2020-12-07 00:27:55 +08:00
proxies = {'http': '127.0.0.1'}
2020-12-05 17:53:38 +08:00
for i in range(10): # pylint: disable=unused-variable
try:
proxy = get_proxy(new)
hku_check(proxy, "Failed get proxy!")
proxies = {'http': 'http://{}'.format(proxy)}
2022-12-16 14:45:52 +08:00
result = requests.get(url, proxies=proxies, timeout=5).text
#hku_info("use proxy: {}".format(proxies['http']))
2020-12-07 00:27:55 +08:00
return result
except Exception as e:
if i == 6:
new = True
2022-12-16 14:45:52 +08:00
time.sleep(2)
2020-12-05 17:53:38 +08:00
raise Exception("无法通过代理访问!")
def request_with_local(url):
"""通过本机ip直接获取请求访问失败将抛出异常"""
return requests.get(url).text