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

代码:

#!/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 = \
    "
" \ "

DNS Resource Records

" \ "
" \ "" \ "" \ "" \ "" \ "" \ "
NameTypeDataTTL
(.*?)(.*?)(.*?)(.*?)
" \ "
" \ "
" regString2 = '

(.*?)

' regString3 = '

(.*?)

' regString4 = "(.*?)(.*?)(.*?)(.*?)" regString5 = '

(.*?)

' 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

效果:


抓取经常访问的网站的最快响应IP_第1张图片
image.png

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

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

你可能感兴趣的:(抓取经常访问的网站的最快响应IP)