python__博客网络爬虫作业回顾

这个爬虫,可以获取首页目录博客第一页所有文章链接,并保存在本地。

"""导入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)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值