一个简单爬免费代理IP的脚本

帮朋友写的。

#!/usr/bin/env python
#encoding:utf-8
#by i3ekr

import requests,re,time
#默认的nn可以更改为nt、wn、wt
xici = "http://www.xicidaili.com/nn/"
port_res = "<td>(\d{0,5})<\/td>"
ips_res = "\d+\.\d+\.\d+\.\d+"
nu = input("你想要跑几页IP(100/页):")
ok = []
head={
        "User-Agent":"User-Agent: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"
        }
for n in range(1,nu):
    url = xici + str(n)
    res_html = requests.get(url,headers=head).text
    ips = re.findall(ips_res,res_html)
    ports = re.findall(port_res,res_html)
    flag = "%s:%s"%(ips[n-1],ports[n-1])
    print "[%s] %s"%(n,flag)
    ok.append(flag)
    time.sleep(0.03)


with open('./result.txt','a') as f:
    for i in ok:
        f.write(i)
    f.close()

猜你喜欢

转载自www.cnblogs.com/nul1/p/8918534.html
今日推荐