获取免费IP--代码--转

#! /usr/bin/env python3
from urllib import request
import random
import time
import lxml
import re



user_agent_list = [
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/45.0.2454.85 Safari/537.36 115Browser/6.0.3',
'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',
'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
'Opera/9.80 (Windows NT 6.1; U; en) Presto/2.8.131 Version/11.11',
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_0) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11',
'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; SE 2.X MetaSr 1.0; SE 2.X MetaSr 1.0; .NET CLR 2.0.50727; SE 2.X MetaSr 1.0)',
'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0',
'Mozilla/5.0 (Windows NT 6.1; rv:2.0.1) Gecko/20100101 Firefox/4.0.1',
]

count = 0

def Get_proxy_ip():
headers = {
'Host': 'www.xicidaili.com',
'User-Agent':'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)',
'Accept': r'application/json, text/javascript, */*; q=0.01',
'Referer': r'http://www.xicidaili.com/',
}
req = request.Request(r'http://www.xicidaili.com/nn/', headers=headers)
response = request.urlopen(req)
html = response.read().decode('utf-8')
proxy_list = []
ip_list = re.findall(r'\d+\.\d+\.\d+\.\d+',html)
port_list = re.findall(r'<td>\d+</td>',html)
for i in range(len(ip_list)):
ip = ip_list[i]
port = re.sub(r'<td>|</td>', '', port_list[i])
proxy = '%s:%s' %(ip,port)
proxy_list.append(proxy)
return proxy_list

def Proxy_read(proxy_list, user_agent_list, i):
proxy_ip = proxy_list[i]
print('当前代理ip:%s'%proxy_ip)
user_agent = random.choice(user_agent_list)
print('当前代理user_agent:%s'%user_agent)
sleep_time = random.randint(1,3)
print('等待时间:%s s' %sleep_time)
time.sleep(sleep_time)
print('开始获取')
headers = {
'Host': 's9-im-notify.csdn.net',
'Origin':'http://blog.csdn.net',
'User-Agent': user_agent,
'Accept': r'application/json, text/javascript, */*; q=0.01',
'Referer': r'http://blog.csdn.net/u010620031/article/details/51068703',
}

proxy_support = request.ProxyHandler({'http':proxy_ip})
opener = request.build_opener(proxy_support)
request.install_opener(opener)

req = request.Request(r'http://blog.csdn.net/u010620031/article/details/51068703',headers=headers)
try:
html = request.urlopen(req).read().decode('utf-8')
except Exception as e:
print('******打开失败!******')
else:
global count
count +=1
print('OK!总计成功%s次!'%count)

if __name__ == '__main__':
proxy_list = Get_proxy_ip()
for i in range(100):
Proxy_read(proxy_list, user_agent_list, i)

代理网站

https://link.jianshu.com?t=http://www.cz88.net/proxy/
https://link.jianshu.com?t=http://www.66ip.cn/index.html 
https://link.jianshu.com?t=http://www.xicidaili.com/
https://link.jianshu.com?t=http://www.coobobo.com/
https://link.jianshu.com?t=http://www.kuaidaili.com/
https://link.jianshu.com?t=http://www.proxy360.cn/default.aspx
https://link.jianshu.com?t=http://ip.zdaye.com/
https://link.jianshu.com?t=https://www.us-proxy.org/
https://link.jianshu.com?t=http://www.nianshao.me/
https://link.jianshu.com?t=http://www.goubanjia.com/
https://link.jianshu.com?t=http://www.iphai.com/
https://link.jianshu.com?t=http://www.meiridaili.com/
https://link.jianshu.com?t=http://www.swei360.com/free/
https://link.jianshu.com?t=http://www.89ip.cn/
https://link.jianshu.com?t=http://www.ip3366.net/
https://link.jianshu.com?t=http://www.mimiip.com/
https://link.jianshu.com?t=http://daili.wy96.com/page1.asp
https://link.jianshu.com?t=http://ip.qiaodm.com/free/index.html
https://link.jianshu.com?t=http://www.ip181.com/
https://link.jianshu.com?t=http://www.httpsdaili.com/free.asp
https://link.jianshu.com?t=http://proxy.mimvp.com/free.php
https://link.jianshu.com?t=http://www.httpdaili.com/mfdl/
https://link.jianshu.com?t=http://www.mayidaili.com/free
https://link.jianshu.com?t=http://www.fengyunip.com/free/china-high.html
https://link.jianshu.com?t=http://www.kxdaili.com/dailiip.html
https://link.jianshu.com?t=http://www.xdaili.cn/freeproxy
https://link.jianshu.com?t=http://www.superfastip.com/welcome/getips
https://link.jianshu.com?t=http://www.chinabaike.com/t/10475/2016/0530/5239251.html

纯真
66免费代理网
西刺免费代理IP
酷伯伯HTTP代理
快代理
proxy360.cn
站大爷
Free Proxy List
年少
全网代理IP
IP海
每日代理
360代理IP
流年免费HTTP代理IP 24小时自助提取系统
云代理
秘密代理IP
迷惘网络IP代理
敲代码 免费代理IP网
代理IP检测平台,100免费代理IP
瑶瑶代理IP
米扑代理
httpdaili
蚂蚁代理
风云代理IP
开心代理
讯代理
急速IP

 

posted @ 2019-05-24 21:14  xiaohaiz  阅读(779)  评论(0编辑  收藏  举报