爪巴虫 1.0版
来源:互联网 发布:库控制软件 编辑:程序博客网 时间:2024/06/15 11:18
无聊之中,写了一个爪巴虫
如你所见,这是一条不称职的爪巴虫
无奈,实力有限,时间有限,精力有限,只能写到这种程度了。
不过也算是我写的第一条爪巴虫
好歹可以算我python已经入门了,对伐,呵呵~
如你所见,这是一条不称职的爪巴虫
无奈,实力有限,时间有限,精力有限,只能写到这种程度了。
不过也算是我写的第一条爪巴虫
好歹可以算我python已经入门了,对伐,呵呵~
貌似效率好低,试了几次,把自己的电脑差点弄挂掉了~
待进一步完善中。。。。
import urllibtry: from urllib.request import urlopenexcept ImportError: from urllib2 import urlopenimport osimport sysfrom copy import deepcopyfrom string import replace;#judge if repeat?def Judge(dirname): lenn=len(repeat); for i in range(0,lenn): if repeat[i]==dirname: return True; repeat.append(deepcopy(dirname)); return False;#不带参数,规范dirnamedef Change(dirname): lenn=len(dirname); g=lenn; for i in range(0,lenn): if dirname[i]=='?': g=int(i) break; ans = dirname[0:g]; return ans; #输入网址def getURL(): global URL; URL=raw_input("enter the URL(no '/' end!!):"); #URL="http://blog.sina.com.cn"; curUrl=deepcopy(URL); #输入保存路径def getPath(): global Path; Path=raw_input("enter the path(no '\\' end):"); #Path="F:\\test" os.chdir(Path);#if startwithdef startwith(test1,test2): len1=len(test1); len2=len(test2); lenn=min(len1,len2); for i in range(0,lenn): if test1[i]!=test2[i]: return False; return True;#得到路径def getHref(text): lent=len(text) #print text[0:5]; for i in range(0,lent-5): if text[i:i+5]=='href=': if text[i+5]!='"' and text[i+5]!="'": dirname=text[i+5]; else: dirname=""; for j in range(i+6,lent): if text[j]=='"' or text[j]=="'" or text[j]=='>': break; else: dirname=dirname+text[j]; getNewUrl(dirname); i=j;#得到新的路径def getNewUrl(dirname): if Judge(dirname): return False; if startwith(dirname,"http://"): if not startwith(dirname,URL): return False; dirname=dirname[len(URL):len(dirname)]; dirname=Change(dirname); if not startwith(dirname,"/") and not startwith(dirname,"../") or dirname[1:]=="": return False; else: prePath=dirname.split('/'); lenn=len(prePath[-1]); for i in range(0,lenn): if prePath[-1][i]=='.': #print '\\'.join(prePath[0:-1:1]); createPath('\\'.join(prePath[0:-1:1])); curUrl=URL+dirname; getCon(URL+dirname,prePath[-1]); os.chdir(Path); return False; createPath('\\'.join(prePath)); #print URL+dirname; getCon(URL+dirname);#写入文件def loadFile(Url,filename): f=open(filename,'w'); data=urlopen(Url); f.write(data.read()); f.close(); #创建路径def createPath(path): if path == '': return False; else: #print path; pp=path.split('\\'); curP=deepcopy(Path); for i in pp: if i=="": os.chdir(Path); return False; if not os.path.exists(curP+"\\"+i): os.mkdir(curP+"\\"+i); else: os.chdir(curP+"\\"+i); curP=curP+"\\"+i; os.chdir(curP); #得到内容def getCon(Url,filename='index.html'): #print Url; try: url=urlopen(Url); except: print "IOError"; return ; loadFile(Url,filename); while True: data=url.readline(); if data=="": break; getHref(data);def unit(): global curUrl; global repeat; global Path; global URL; URL=""; repeat=list(); if __name__=='__main__': unit(); getURL(); getPath(); getCon(URL); print "robot finish the work now!!!";
- 爪巴虫 1.0版
- Python 爪巴虫 2.0版
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- 1.0.61.686 版发布
- C#版 泡泡堂 1.0
- 项目1.0版诞生
- robocode机器人1.0版
- 聊天室1.0版
- FireFox 1.0预览版发布
- BitTorrent 协议规范1.0版
- BaseTestCase system 1.0 体验版
- SQLite 3 中的数据类型
- 企业网站内容缘何不被收录
- ImageMagick 从内存数据创建 Image
- log4j.xml文件详解
- 汇编学习笔记-一段安全的空间及段前缀的使用.
- 爪巴虫 1.0版
- Builder模式的误区:将复杂对象的构建进行封装,就是Builder模式了吗?
- MicroCoder flashSlider——基于Jquery的图片展示插件
- 【转载】Web Service定义
- 黑马程序员_IO回顾02
- 随机出题1.0版——random和open的使用
- 编译原理书籍推荐
- 网络中划分超网的一点理解
- MySql的Join使用