首页 > 编程知识 正文

小爬爬贼能爬,轮子哥是什么意思

时间:2023-05-05 05:15:32 阅读:150180 作者:148

有时候上车轮哥的博客上网条件不好,干脆上车轮哥的博客在线看。 以前读了他的正则引擎的文章,自己也写了简单的正则引擎,收获很多。 有了正则工具,构建可配置词法分析器就很方便了。 然后,你会想做编译器吧。 因此,我们必须开始制作语法分析器。 然后,必须生成代码,并设计虚拟机程序集以使代码奔跑。 总之我在提高自己的能力。 我打心底里觉得车轮博客的文章质量很高。 我有一个愿望,就是尽可能地遍历车轮哥哥写的项目。 没必要达到那么高的水平。 我要是知道这是怎么做的就好了。 我觉得学习所有制作这个的想法就ok了。

python是我现在正在学习的东西,这个小程序的难度很低,但是很麻烦。

----- coding : utf-8---importshutilimporturllib2importreimportosdefgetcss (3360 ) CSS文件(response=ur lib2. ur ) pattern=re.compile(r's*.CSS' ) s=re.find all (pattern,res ) forcs sins : n=len (CSS-1 CSS ) 3360 CSS full path=' http://www.cppblog.com ' CSS else : CSS full path=CSS response=ur lib2. urlopen (CSS full path ) RES path . css ' ) filename=re.findall ) pattern,css ) print ' this is filename ' print file nent ' WB ' ) fo.write(RES ) fo.close ) url ) html name=result [0] if (OS.path.isfile (dirname '/' html name ) ) :打印' file exist 3360 ', dirname '/' htmlnamereturnresponse=urllib2. urlopen (URL ) res=response.read ) pattern=re.compile(r ) ) ) dide ' ' (a=re.findall(pattern,res ) downloadimgsrcpattern=re.com pile (r ' src=(http://) )? .jpg|. ((png ) ) ') pic=re.findall ) pattern,a[0] ) if(pic==[] ) :打印' no pictures 3360 ' madd src=a [0] forc . png ' (imgname=re.find all (img pattern, item ) imgpath=dirname'/'imgname[0] if(OS.path.isfile(imgpath )==False ) :f=urlib2.urlopen ) item 'wb ' ) ascode:code.write(data ) madd imgname[0] ) print 'Downlod picture: )、imgpath else 3360 print ' img exist : ' # genhtmlfo=open (' head.html ',)

t;/body></html>" maddsrc = str + maddsrc + tail fo.close() filename = dirname+'/'+htmlname if(os.path.isfile(filename)==False): fo = open(filename,"wb") fo.write(maddsrc) fo.close() print "Generate article :",filename else: print "File exist:" returndef getCategory(url): "sort by category" htmpattern = re.compile(r'w*.html') result = re.findall(htmpattern,url) htmlname = result[0] url=url+"?Show=All" response = urllib2.urlopen(url) res = response.read() pattern = re.compile(r'(<div class="entrylist">[dD]*)<div class="entrylistItemPostDesc">') a = re.findall(pattern,res) if(a==[]): print "Failed:",url return fo = open("head.html","r+") str = fo.read() tail = "</div></body></html>" maddsrc = str + a[0] + tail fo.close() filename = htmlname print "nn======= Generate Category html:",filename #get the essay url pattern = re.compile(r'href="(http://www.cppblog.com/vczh/archive/w*/w*/w*/w*.html)"') res = re.findall(pattern,maddsrc) for item in res: pattern = re.compile(r'http://www.cppblog.com/vczh/archive/w*/w*/w*/(.+?.html)') crec = re.findall(pattern,item) dirname = filename[0:len(filename)-5] if(os.path.isdir(dirname)==False): os.mkdir(dirname) print "Make directory:",dirname maddsrc = maddsrc.replace(item,dirname+'/'+crec[0]) fo = open(filename,"wb") fo.write(maddsrc) fo.close() #signleFileHandle(item,dirname) return## 主逻辑def getHtml(): "sort by category" response = urllib2.urlopen("http://www.cppblog.com/vczh/default.html?page=1&OnlyTitle=1") res = response.read() pattern = re.compile(r'(http://www.cppblog.com/vczh/category/d*.html)"') category = re.findall(pattern,res) for item in category: getCategory(item) returndef genIndexfile(): "生成首页" fo = urllib2.urlopen("http://www.cppblog.com/vczh/default.html?page=1&OnlyTitle=1") src = fo.read() pattern = re.compile(r'rel="external nofollow" href="http://www.cppblog.com/vczh/category/(.*?.html">)') clist = re.findall(pattern,src) print clist pattern = re.compile(r'rel="external nofollow" href="http://www.cppblog.com/vczh/category/d*.html">(.+?)</a>') nlist = re.findall(pattern,src) fo.close() fo = open("head.html","r+") str = fo.read() fo.close() fo = open("index.html","wb") tail = "</div></body></html>" for i in range(0,len(nlist)): str=str+"<li><a href=""+clist[i]+nlist[i]+"</a></li>" str=str+tail fo.write(str) fo.close()returngetCss()getHtml()

执行这段代码,会在当前目录生成目录文件夹和目录对应的html,以及在目录文件夹下,会把该目录下的文章都下载下来,并且删掉多余的标签,只保留正文部分。首页的话自己改一下genIndexfile()里的内容就行了。

里边用到的head.html我是这样写的,图个简便。

<html><head id="Head"><title></title><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><link type="text/css" rel="stylesheet" rel="external nofollow" href="common.css" /><link id="MainCss" type="text/css" rel="stylesheet" rel="external nofollow" href="style.css" /></head><body>

然后要把当前目录下的css文件复制到生成的每个目录中去,因为写完了我才发现head.html里css路径我没改,故作此亡羊补牢之措。把下面代码保存为mvcss.sh,然后$chmod 777 mvcss.sh&&./mvcss.sh

#!/bin/bashfor dirname in `ls -F|grep "/"`do echo $dirname cp *.css $dirname ls -a $dirname|grep cssdone

目录截图:

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。