import requests
from lxml import etree


def getproxy():
    '''''
    获取代理ip
    '''''
    ips = []
    i = 0
    url = "https://www.toolbaba.cn/ip"
    html = etree.HTML(requests.get(url).text)
    ip = html.xpath('/html/body/div/div/div/div/table/tbody/tr/td[1]/text()')
    port = html.xpath('/html/body/div/div/div/div/table/tbody/tr/td[2]/text()')
    for x in ip:
        ips.append(str(x)+':'+str(port[i]))
        i += 1
    x = None
    i = 0
    '''''
    第二个站点的获取
    '''''
    url = "https://free.kuaidaili.com/free/inha/"
    html = etree.HTML(requests.get(url).text)
    ip = html.xpath(
        '/html/body/div/div/div/div/div/table/tbody/tr/td[1]/text()')
    port = html.xpath(
        '/html/body/div/div/div/div/div/table/tbody/tr/td[2]/text()')
    for x in ip:
        ips.append(str(x)+':'+str(port[i]))
        i += 1
    return ips


def infoproxy(proxy):
    '''''
    查询代理IP是否可用
    '''''
    proxies = {'https': proxy}
    try:
        code = requests.get('https://www.baidu.com/',
                            proxies=proxies, timeout=10).status_code
        if str(code) == '200':
            return True
        else:
            return False
    except Exception:
        return False


def main():
    pass
