这个爬虫,可以获取首页目录博客第一页所有文章链接,并保存在本地。
"""导入urllib2,定义url[]收集每个blog地址"""
import urllib2
url=[]
"""定义一个函数,进行内部迭代,获取全部地址并返回"""
def geturl(con,begin=1):
"""根据html源码分析,都为<a title=*href=*.html>,解析出其中blog的地址"""
title = con.find(r'<a title=',begin)
href=con.find(r'href=',title)
html=con.find(r'.html',href)
if html>0:
url.append(con[href+6:html+5])
geturl(con,html)
else :
return url
"""获取博文目录首地址,为解析对象"""
con=urllib2.urlopen('http://blog.sina.com.cn/s/articlelist_1191258123_0_1.html').read()
geturl(con)
"""对url进行迭代,以倒数26字节起到末尾命名贮存在本地"""
for x in url:
http=urllib2.urlopen(x).read()
content=x[-26:]
print content
open(r'hanhan/'+content,'w').write(http)