a-z

do you never to do

博客园 首页 新随笔 联系 订阅 管理
#coding=utf-8       #新浪博客     
import urllib
import re
import os
url=['']*1500 #每一骗博客的地址
title=['']*1500  #每一篇博客的标题
page=1  #博客分页
count=1  #文章计数
while page<=9:
	con=urllib.urlopen('http://blog.sina.com.cn/s/articlelist_1193491727_0_'+str(page)+'.html').read()
	i=0	
	hrefstart=con.find(r'href="http://blog.sina.com.cn/s/blog_')
	print hrefstart
	hrefend=con.find(r'.html',hrefstart)
	print hrefend
	titlestart=con.find(r'>',hrefend)
	print titlestart
	titleend=con.find(r'</a>',titlestart)
	print titleend

	while i<=50 and titleend!=-1 and hrefend!=-1:
		url[i]=con[hrefstart+6:hrefend+5]
		title[i]=con[titlestart:titleend]
		print page,i,count, title[i]
		print url[i]
		hrefstart=con.find(r'href="http://blog.sina.com.cn/s/blog_',titleend)
		hrefend=con.find(r'.html',hrefstart)
		titlestart=con.find(r'>',hrefend)
		titleend=con.find(r'</a>',titlestart)
		content=urllib.urlopen(url[i]).read()
		filename=url[i][-26:]
		print filename
		if not os.path.isdir("1"):
		   os.mkdir("1")
		target=open('1/'+filename,'w')
		target.write(content)
		i=i+1
		count=count+1		
	else:
		print page,'本页查找到结尾了'
	page=page+1
else:
	print'本次任务结束了'

用python2.7,采集新浪博客,王石的博客文章。

实现了文章列表多页采集,实现了下载到本地。

练手之做,如果有更好的代码,也分享一些给我

欢迎交流  

还有几点未作:

1、利用正则实现提取每一页的文章内容。

2、目录按照下载时间自动命名

 

posted on 2015-10-04 11:21  a-z  阅读(828)  评论(0)    收藏  举报