python+selenium+chromedriver抓取shodan搜索结果

作用:免积分抓取shodan的搜索结果,并把IP保存为txt

前提:

  ①shodan会员(ps:黑色星期五打折)

  ②安装有python27

  ③谷歌浏览器(ps:版本一定要跟chromedriver匹配)

  ④windows系统

开始:

一.安装好必要的包

  ①win+R 调用cmd

  ②cd C:\Python27\Scripts(ps:以你自己实际安装目录来)

  ③pip install selenium

  ④pip install pyquery

二.下载核心组件和脚本

  ①shodan_project.zip 并且把解压到C:\Python27\

  ②chromedriver 解压进C:\Python27\shodan_project (ps:版本要跟谷歌浏览器对应,不然会导致抓取失败)

使用教程:

  ①修改shodan账号密码,和你要搜索的关键字

  ②python shodan_main.py 出现以下画面说明成功运行

 

  ③结果保存在success.txt

 

缺点:

  ①由于原作者是写死只抓取<a href="http://.*">格式的ip,会导致很多结果无法抓取出现getipfail<type'exceptions,Exception>的情况。

       ②然而shodan搜到的ip会有https,/host/,http等多种情况。你可以根据自己的情况修改源码。或者等我学习爬虫后出个升级版

 

临时解决办法:

   ①打开shodan_main.py,改为下面的语法

ip_item = re.findall(r'<a href=".*">', contents) 三个格式都抓取

  ②然而这样会导致下面的情况,把http://,/host/也给搞了进来

  ③我们可以利用记事本的替换功能,点击全部替换。

 

改进:

  ①bat指定Notepad++打开shodan_main.py

start /d "C:\Program Files (x86)\Notepad++" notepad++.exe "C:\Python27\shodan_project\shodan_main.py"

 

  ②bat一键启动shodan_main.py

@echo off  
cd  C:\Python27\shodan_project

start python shodan_main.py

exit

   ③bat打开结果目录

start explorer "C:\Python27\shodan_project"

 2018/4/28更新:

  ①把keyword=的""改为',这样才能搜字符串

shodan_seach(keywords='6379 country:"US"')               #关键字

    ②修改re.sub替换函数

ip = re.sub('/host/|http://|https://|">', "", ip)   # |是或的意思,这样就不用手动替换了

 2018/4/29更新:

  ①项目添加clean.py,过滤success.txt里的个别乱码

#!/usr/bin/env python
#_*_coding:utf-8 _*_
__author__ = 'gaogd'
import  re
 
with open('success.txt','r') as f:
    for line in f.readlines():
        result2 = re.findall('[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}', line)
        if not result2  == []:
            print result2[0]
            result = result2[0] + '\n'
            with open('arr_ip.txt', 'a+') as w:
                w.write(result)

  ②修改打开结果的bat

@echo off  
cd  C:\Python27\shodan_project

start python clean.py  '打开结果前运行该脚本

start explorer "C:\Python27\shodan_project"

exit                          'arr_ip.txt就是过滤后干净的ip

 

 

感谢:

  lethelife.com

参考:

  ①Python 从文件中筛选出ip 正则表达

  ②如何用python的re.sub( )方法进行“多处”替换

posted @ 2018-04-27 18:47  左小龙  阅读(497)  评论(0编辑  收藏  举报