0x 00 前言

    前天自己在玩的时候,自己通过百度搜索主机的二级域名感觉好麻烦,自已要一页页的去翻

    而且人工识别是否是重复的二级域名也够蛋疼的,正好最近在学正则表达式,权当练手了

0x 00 代码

  

# coding=utf-8
# author:Anka9080
# environment:Eclipse
import urllib
import urllib2
import cookielib
import re #site = 'baidu.com'
print 'Please input the root site like "baidu.com":'
site = raw_input()
siteFormat1 = site
siteFormat1 = siteFormat1.replace('.', '\.')
#print siteFormat1 urlPage = 'http://www.haosou.com/s?src=360sou_newhome&q=site:'+site
req = urllib2.Request(urlPage)
res = urllib2.urlopen(req)
html = res.read().decode('utf-8')
# 获得搜索结果的页面数
pageStr = re.search(ur'找到相关结果约(.*?)个',html)
page = pageStr.group(1)
formatNum = ''
for c in page:
if not c in formatNum:
page = page.replace(c,'')
page = int(page) / 10
print 'Total Page: ' + str(page) if page > 6:
page = 6
newItems = []
for p in range(1, page):
urlDomain = 'http://www.haosou.com/s?src=360sou_newhome&q=site:'+site+'&pn='+str(p)
req = urllib2.Request(urlDomain)
res = urllib2.urlopen(req)
html = res.read().decode('utf-8')
tmp = 'linkinfo\"\>\<cite\>(.+?\.'+siteFormat1+')';
pattern = re.compile(tmp)
items = re.findall(pattern, html) # 去重操作
for item in items:
if item not in newItems:
newItems.append(item) print 'SubDomain Count: '+ str(len(newItems) - 1) for item in newItems: # 获得对应 IP 信息
pattern = re.compile(ur'\>\>\ (.*?)\<\/font[\s|\S]*?本站主数据:(.*?)\<\/li\>')
urlIP = 'http://www.ip138.com/ips138.asp?ip='+item
req = urllib2.Request(urlIP)
res = urllib2.urlopen(req)
html = res.read().decode('gb2312')
result = re.search(pattern,html)
print item + ' ' + result.group(1) + ' ' + result.group(2)

  测试结果如下:

  

Please input the root site like "baidu.com":
baidu.com
Total Page: 2
SubDomain Count: 9
www.baidu.com 61.135.169.121 北京市 百度蜘蛛 联通
tieba.baidu.com 123.125.65.93 北京市 联通
fanyi.baidu.com 202.108.23.153 北京市 联通
wenku.baidu.com 123.125.70.102 北京市 百度蜘蛛 联通
map.baidu.com 112.80.248.48 江苏省南京市 联通
music.baidu.com 123.125.114.14 北京市 联通
zhidao.baidu.com 123.125.65.91 北京市 联通
baike.baidu.com 123.125.70.105 北京市 百度蜘蛛 联通
yun.baidu.com 123.125.65.51 北京市 联通
pan.baidu.com 202.108.23.29 北京市 联通

0x 02 总结

    思路大概是这个样子:

    先通过urllib2.Request()urllib2.urlopen()访问url

    再从返回结果中得到搜索结果页面数

    为了提高效率 页面数 大于 5 会只爬行搜索结果的前5个页面

    后面 又做了去重操作 然后就得到二级域名列表咯 : )

    中间蛋疼的 地方倒是 Py 的 转义符号问题  身边能有个可以问问的大牛多好~

    后期 准备使用 http://dns.aizhan.com/的查询结果 直接获得 IP以及旁站信息

    ==================6.13号更新====================

    在知乎上请教后已经解决转义问题,之前的逻辑没有理清导致出错,和编码并没有神马关系(晚上敲代码很容易出错哈 ⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄

    现在已经可以查出二级域名对应的IP地址以及地理位置信息

    感觉http://dns.aizhan.com 的调用比较麻烦,接口已经换成 http://www.ip138.com

文中图片引自:http://developer.51cto.com/art/201403/431104.htm(原博客链接失效)

    

『Python』爬行搜索引擎结果获得指定主机二级域名及IP信息的更多相关文章

  1. 『Python』__getattr__()特殊方法

    self的认识 & __getattr__()特殊方法 将字典调用方式改为通过属性查询的一个小class, class Dict(dict): def __init__(self, **kw) ...

  2. 『Python』库安装

    1.安装指定版本的tensorflow 虽然官网有4种安装方式,并且推荐用anaconda的方式,但是有时候我们需要指定版本的tensorflow,而pip可以做到. 比如我装的是anaconda3. ...

  3. 『Python』多进程处理

    尝试学习python的多进程模组,对比多线程,大概的区别在: 1.多进程的处理速度更快 2.多进程的各个子进程之间交换数据很不方便 多进程调用方式 进程基本使用multicore() 进程池优化进程的 ...

  4. 『Python』源码解析_从ctype模块理解对象

    1.对象的引用计数 从c代码分析可知,python所有对象的内存有着同样的起始结构:引用计数+类型信息,实际上这些信息在python本体重也是可以透过包来一窥一二的, from ctypes impo ...

  5. 『Python』进程同步

    1. Lock(互斥锁) 是可用的最低级的同步指令.Lock处于锁定状态时,不被其他的线程拥有. from multiprocessing import Process, Value, Lock de ...

  6. 『Python』多进程

    Python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在Python中大部分情况需要使用多进程.Python提供了multiprocessin ...

  7. 『Python』面向对象(二)

    继承 继承的语法 class Animal(object): def __init__(self,name): self.__name = name class Dog(Animal): kind = ...

  8. 『Python』 爬取 WooYun 论坛所有漏洞条目的相关信息

    每个漏洞条目包含: 乌云ID,漏洞标题,漏洞所属厂商,白帽子,漏洞类型,厂商或平台给的Rank值 主要是做数据分析使用:可以分析某厂商的各类型漏洞的统计:或者对白帽子的能力进行分析..... 数据更新 ...

  9. 『Python』 ThreadPool 线程池模板

    Python 的 简单多线程实现 用 dummy 模块 一句话就可以搞定,但需要对线程,队列做进一步的操作,最好自己写个线程池类来实现. Code: # coding:utf-8 # version: ...

随机推荐

  1. 加载GIF动画方法 iOS

    方法一 使用UIWebView _codeStr为gif网址      如果是本地的gif可以直接使用dataWithContentsOfFile方法 NSData *data = [NSData d ...

  2. [PWA] 8.Unobtrusive update: Delete old cache and only keep one, hard refresh to let new SW to take control

    So once you modify the code, service worker will auto create a new one and it won't take control ove ...

  3. RabbitMQ Management HTTP API--官方文档

    Introduction Apart from this help page, all URIs will serve only resources of type application/json, ...

  4. [转] linux之sed用法

    sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作,下面先了解一下sed的用法sed命令行格式为:         sed ...

  5. RunTime 应用实例–关于埋点的思考

    埋点是现在很多App中都需要用到的,这个问题可能每个人都能处理,但是怎样来减少埋点所带来的侵入性,怎样用更加简洁的方式来处理埋点问题,怎样减少误埋,如果上线了发现少埋了怎么办?下面是本文讨论的重点: ...

  6. Linux开发工具之gcc

    一.gcc入门(上)   1.gcc相关概念   gcc(GNU C Compiler)编译器,最初支持C语言,现已支持C.C++.Java.Pascal.Ada.COBOL语言等:支持多种硬件平台: ...

  7. oracle中的function 、procedure、packages、package bodies比较

    1  function和procedure的区别 1).可以理解函数是存储过程的一种 2).函数可以没有参数,但是一定需要一个返回值,存储过程可以没有参数,不需要返回值 3).函数return返回值没 ...

  8. Asp.net页面使用showModalDialog时Postback弹出新页面解决办法

    今天碰到一个让我一开始觉得莫名其妙的问题, 用window.showModalDialog打开一个.aspx文件,然后点击这个页面上一个button, 把页面的数据存入数据库之后,居然又打开一个这个页 ...

  9. ie浏览器下input和select的上下居中问题!!!!

    在Google浏览器下的input和select标签里面的文字是根据它的高度自适应上下居中的,而ie浏览器下的input和select里面的文字就不会根据高度自适应上下居中,跟大家分享一下我的解决方法 ...

  10. angular-ui-tree

    angular-ui-tree的github项目地址:https://github.com/angular-ui-tree/angular-ui-tree DEMO目录结构如下: bootstrap. ...