Python爬取韩寒全部新浪博客
2016-03-07 22:45
459 查看
接上一篇,我们根据第一页的链接爬取了第一页的博客,我们不难发现,每一页的链接就只有一处不同(页码序号),我们只要在上一篇的代码外面加一个循环,这样就可以爬取所有博客分页的博文,也就是所有博文了。
代码最下面部分,保存网页只能保存到50,不知道错在哪里。
所以就将保存网页的代码放在搜索里,找到就保存!
正确运行界面:
运行结果:
# -*- coding : -utf-8 -*- import urllib import time url = [' ']*350 page = 1 link = 1 while page <=7://目前共有7页,3 con = urllib.urlopen('http://blog.sina.com.cn/s/articlelist_1191258123_0_'+str(page)+'.html').read() i = 0 title = con.find(r'<a title=') href = con.find(r'href=',title) html = con.find(r'.html',href) while title != -1 and href != -1 and html != -1 and i<350: url[i] = con[href + 6:html + 5] content = urllib.urlopen(url[i]).read() open(r'allboke/'+url[i][-26:],'w+').write(content) print 'link',link,url[i] title = con.find(r'<a title=',html) href = con.find(r'href=',title) html = con.find(r'.html',href) i = i + 1 link = link + 1 else: print 'page',page,'find end!' page = page + 1 else: print 'all find end' #i = 0 #while i < 350: #content = urllib.urlopen(url[i]).read() #open(r'save/'+url[i][-26:],'w+').write(content) #print 'downloading',i,url[i] #i = i + 1 #time.sleep(1) #else: print 'download artical finished!'
代码最下面部分,保存网页只能保存到50,不知道错在哪里。
所以就将保存网页的代码放在搜索里,找到就保存!
正确运行界面:
运行结果:
相关文章推荐
- Python爬虫模拟登录京东获取个人信息
- Pyenv/pyenv-virtualenv/jupyter的部署/安装/使用
- python的join用法
- python学习笔记-Day17 - web框架
- Python爬虫爬取一页韩寒新浪博客
- python splinter安装与使用
- Python与硬件学习笔记:蓝牙(二)
- Python 字符串 String 内建函数大全(2)
- Python与硬件学习笔记:蓝牙(一)
- python成长之路第三篇(3)_内置函数及生成器迭代器 [面试]
- python学习笔记-Day17 (上节知识点补充)
- Python爬取拉勾网招聘信息
- Python文档生成工具pydoc使用介绍
- Python学习笔记一:列表和元组
- Python学习(三) 八大排序算法的实现(上)
- 集体智慧编程——搜索与排名-Python实现
- 验证码图像识别-python
- python 系列之 - 异常处理
- win8安装python2.7.msi出错的解决方法
- python的性能优化