# -*- coding: utf-8 -*-
# @Time    : 2018/3/26 15:54
# @Author  : glacier
# @Email   : 2284711614@qq.com
# @File    : Proxy-hot.py
# @Software: PyCharm


from bs4 import BeautifulSoup
import requests
import random
from urllib import request

def get_ip_list(url, headers):
    web_data = requests.get(url, headers=headers)
    soup = BeautifulSoup(web_data.text, 'html.parser')
    ips = soup.find_all('tr')
    ip_list = []
    for i in range(1, len(ips)):
        ip_info = ips[i]
        tds = ip_info.find_all('td')
        ip_list.append(tds[1].text + ':' + tds[2].text)
    return ip_list

def get_random_ip(ip_list):
    proxy_list = []
    for ip in ip_list:
        proxy_list.append('http://' + ip)
    proxy_ip = random.choice(proxy_list)
    proxies = {'http': proxy_ip}
    return proxies

if __name__ == '__main__':
    url = 'http://www.xicidaili.com/nn/'
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36'
    }
    ip_list = get_ip_list(url, headers=headers)
    proxies = get_random_ip(ip_list)
    print(proxies)

    try:

        web_data = requests.get('https://ip.cn/', headers=headers, proxies=proxies,timeout=10).text
        soup = BeautifulSoup(web_data, "html.parser")
        ip = soup.find('div',{'class':'well'})
        print(ip)
    except:
        proxies = get_random_ip(ip_list)
        web_data = requests.get('https://ip.cn/', headers=headers, proxies=proxies, timeout=10).text
        soup = BeautifulSoup(web_data, "html.parser")
        ip = soup.find('div', {'class': 'well'})
        print(ip)











