1. 程式人生 > >python實現同服站點地址獲取

python實現同服站點地址獲取

.cn class return 字符串 ... urlencode 一個 req exc


說明:程序使用http://s.tool.chinaz.com/same此站點查詢的結果。使用python簡單的實現抓取結果

先隨便查詢一個結果,抓包分析,如圖:

技術分享


使用python模仿post表單,使用正則表達式匹配結果

代碼例如以下:

# -*- coding: utf-8 -*- 
import urllib
import urllib2
import re
import sys

#get url in the same ip
def get_url(url):
    #set header info
    headers = {  
               'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.116 Safari/537.36',
               'Referer': 'http://s.tool.chinaz.com/same'  
               }
    postdata = urllib.urlencode({'s':url})
    req = urllib2.Request('http://s.tool.chinaz.com/same',postdata,headers)
    try:
        result = urllib2.urlopen(req)
    except:
        print 'Failed to open url,you can try again...'
        return
    fweb = result.read()
    #.</span> <a href='http://www.31hzp.com'
    pattern = re.compile(r'</span> <a href=\'(.+?)\'')
    match = pattern.findall(fweb)
    filename = str(url).replace(':', '').replace('\\', '')
    fp = open(filename+'.txt','w')
    if match:
        for m in match:
            fp.write(m)
            fp.write('\n')
            print m  
    else:
        print 'find nothing...'
    fp.close()
#usage
def usage(name):
    #www.31jmw.com
    print '%s www.xxx.com'%name
    sys.exit(1)
#entry point
if __name__ == '__main__':
    if len(sys.argv) != 2:
        usage(sys.argv[0])
    print 'start...'
    url = "".join(sys.argv[1])   #取出列表中的字符串
    #print url
    get_url(url)
    print 'end...'

測試結果例如以下:

F:\mycode\python\pytest\src>ipsamescan.py www.31jmw.com
start...
http://www.31hzp.com
http://100ec.cn
http://ec100.cn
http://toocle.cn
http://www.31jmw.com
http://www.31expo.com
http://www.toocle.cn
http://561288.com
http://www.toocle.com.cn
http://www.31metals.com
http://31expo.com
http://www.100ec.cn
end...


python實現同服站點地址獲取