美文网首页Python
抓取经常访问的网站的最快响应IP

抓取经常访问的网站的最快响应IP

作者: 十一岁的加重 | 来源:发表于2018-05-10 22:10 被阅读13次

代码:

#!/usr/bin/python
# coding=utf-8
import requests
import os
import re

urls = []
outputString = ''
hosts = [
    'Github.com',
    'github.global.ssl.fastly.net',
    'weex.apache.org',
    'Cnblogs.com',
    'Jobbole.com',
    'aliyun.com',
    'Segmentfault.com',
    'Csdn.net',
    'Jianshu.com',
    'weex-project.io',
    'oschina.net',
    ]
regString = \
    "<section class=\"panel2\">" \
    "<h2 class=\"panel-head\">DNS Resource Records</h2>" \
    "<div class=\"panel-body\">" \
    "<table class=\"table table-striped table-h table-dnsrr panel-item\">" \
    "<thead><tr><th>Name</th><th>Type</th><th>Data</th><th>TTL</th></tr></thead>" \
    "<tbody id=\"dnsinfo\">" \
    "<tr><td>(.*?)</td><td>(.*?)</td><td><a href=\"(.*?)\">(.*?)</a></td><td>(.*?)</td></tr>" \
    "</tbody>" \
    "</table>" \
    "</div>" \
    "</section>" 
regString2 = '<h3 class=\"panel-head panel-folder\"><span class=\"icon icon-flag-(.*?)\"></span>(.*?)</h3>'
regString3 = '<h3 class=\"panel-head\"><span class=\"icon icon-flag-(.*?)\"></span>(.*?)</h3>'
regString4 = "<tr><td>(.*?)</td><td>(.*?)</td><td><a href=\"(.*?)\">(.*?)</a></td><td>(.*?)</td></tr>"
regString5 = '<h3 class=\"panel-head panel-folder\" data-fold=\"true\"><span class=\"icon icon-flag-(.*?)\"></span>(.*?)</h3>'

for host in hosts:
    url = 'http://' + host + '.ipaddress.com/#ipinfo'
    urls.append(url)
    html = requests.get(url)
    ips = re.findall(regString, html.text, re.S)
    if len(ips) == 0:
        ips = re.findall(regString2, html.text, re.S)
        if len(ips) == 0:
            ips = re.findall(regString3, html.text, re.S)
            if len(ips) == 0:
                ips = re.findall(regString4, html.text, re.S)
                if len(ips) == 0:
                    pass
                else:
                    twoIps = [ips[0][3]]
                    if len(ips) >= 2:
                        twoIps = [ips[0][3], ips[1][3]]
                    for ip in twoIps:
                        outputString = outputString + ip + ' ' + host + '\n'
            else:
                twoIps = [ips[0][1]]
                if len(ips) >= 2:
                    twoIps.append(ips[1][1])
                for ip in twoIps:
                    outputString = outputString + ip + ' ' + host + '\n'
        else:
            twoIps = [ips[0][1]]
            if len(ips) == 1:
                foldedIps = re.findall(regString5, html.text, re.S)
                if len(foldedIps) >= 1:
                    twoIps.append(foldedIps[0][1])
            elif len(ips) >= 2:
                twoIps.append(ips[1][1])
            for ip in twoIps:
                outputString = outputString + ip + ' ' + host + '\n'
    else:
        twoIps = [ips[0][3]]
        if len(ips) >= 2:
            twoIps.append(ips[1][3])
        for ip in twoIps:
            outputString = outputString + ip + ' ' + host + '\n'

print outputString

效果:


image.png

意义:
这样就不用,手动去找这些常访问的网站的IP了,放到hosts文件里。
刷新DNS:

sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder;say flushed

相关文章

  • 抓取经常访问的网站的最快响应IP

    代码: 效果: 意义:这样就不用,手动去找这些常访问的网站的IP了,放到hosts文件里。刷新DNS:

  • python爬虫批量抓取ip代理

    使用爬虫抓取数据时,经常要用到多个ip代理,防止单个ip访问太过频繁被封禁。 ip代理可以从这个网站获取:http...

  • 关于代理ip与网络爬虫

    为什么我们的ip会被网站禁止访问了呢?网站禁我们ip的理由是什么? 网站封禁IP的理由就是后台查看该IP的访问次数...

  • 一步一步打造爬虫代理池

    最近在使用爬虫爬取数据时,经常会返回403代码,大致意思是该IP访问过于频繁,被限制访问。限制IP访问网站最常用的...

  • ip转换为长整数(带源码)

    简介:访问一个网站除了直接访问127.0.0.1这种类型的ip,还可以通过ip转换的长整数来访问网站。 例子:把a...

  • 30个Linux Shell脚本经典案例(中)

    1、iptables自动屏蔽访问网站频繁的IP 场景:恶意访问,安全防范 2、判断用户输入的是否为IP地址 IP ...

  • 为什么代理ip能够不受限制?

    网络ip不能够访问网站之后,为什么代理ip就能够继续访问呢?换ip之后就恢复正常了吗? 为什么代理ip在网络访问上...

  • 高并发如何避免重复记录用户信息

    高并发如何避免重复记录用户信息 用户访问网站记录用户IP,来源网站,访问网站 高并发如何避免重复记录用户IP地址 ...

  • 刷新本地DNS缓存

    问题描述 为了提高网站的访问速度,系统会在成功访问某网站后将该网站的域名、IP地址信息缓存到本地。下次访问该域名时...

  • 网络相关

    内网IP 和 外网IP 外网IP就是别人访问你网站一般就是访问此IP下面的内容。一般解析都是解析此IP。内网IP就...

网友评论

    本文标题:抓取经常访问的网站的最快响应IP

    本文链接:https://www.haomeiwen.com/subject/onatdftx.html