Python写爬虫-爬甘农大学校新闻
Python写网络爬虫(一)
关于Python:
学过C. 学过C++. 最后还是学Java来吃饭.
一直在Java的小世界里混迹.
有句话说: “Life is short, you need Python!” 翻译过来就是: 人生苦短, 我用Python
到底它有多么强大, 多么简洁?
抱着这个好奇心, 趁不忙的几天. 还是忍不住的小学了一下.(- - 事实上学了还不到两天)
随便用一个"HelloWorld"的样例
- //Java
- class Main{
- public static void main(String[] args){
- String str = "HelloWorld!";
- System.out.println(str);
- }
- }
- #Python
- str = 'HelloWorld'
- print str
乍一看Python确实非常爽! 简明了断 节省时间
至于效率. 我相信无论是不论什么语言, 作为开发人员重要是的还是思维的精简!
也有人说: "Python一时爽, 重构火葬场"
可是Python的应用场景那么多. 依据自己的须要来选择, 相信那么多前辈没错.
Python的确是一门值得学习的课程.
关于网络爬虫:
Python基础还没学完, 就迫不及待的想做个东西来看看
于是就想到了写个网络爬虫. 来小小的练习一下
我的母校是甘肃农大. 我会常常看学校新闻, 于是就试着爬个学校新闻, 没事的时候拿出来看看
由于学的甚少...这个爬虫的功能还是很easy. 就是把学校官网中最新的新闻下载下来, 保存成网页. 想看多少页都能够.
这里我就抓了最新的前4页新闻
第一次写爬虫. 一个非常easy的功能, 我把它分了3步:
第一步: 先爬一条新闻, 把它下载保存!
第二部: 把这一页的全部新闻都爬下来, 下载保存!
第三部: 把x页的全部新闻爬下来, 下载保存!
网页爬虫非常重要的一点就是分析网页元素.
第一步: 先来爬一个url的内容
- #crawler: 甘肃农业大学新闻网板块的->学校新闻.
- #爬这个页面的第一篇新闻
- #http://news.gsau.edu.cn/tzgg1/xxxw33.htm
- #coding:utf-8
- import urllib
- str = '<a class="c43092" href="../info/1037/30577.htm" target="_blank" title="双联行动水泉乡克那村工作组赴联系村开展精准扶贫相关工作">双联行动水泉乡克那村工作组赴联系村开展精准扶贫相关工作</a>'
- hrefBeg = str.find('href=')
- hrefEnd = str.find('.htm', hrefBeg)
- href = str[hrefBeg+6: hrefEnd+4]
- print href
- href = href[3:]
- print href
- titleBeg = str.find(r'title=')
- titleEnd = str.find(r'>', titleBeg)
- title = str[titleBeg+7: titleEnd-1]
- print title
- url = 'http://news.gsau.edu.cn/' + href
- print 'url: '+url
- content = urllib.urlopen(url).read()
- #print content
- filename = title + '.html'
- #将抓取的页面content写入filename保存本地文件夹中
- open(filename, 'w').write(content)
这个里面不用分析太多网页元素. 就字符串拼阿截阿就好了.
第二步: 爬取本页面的全部新闻, 每页23篇
这个时候就要小小的分析下, 这23个url, 每一个url怎么找?
这里能够先锁定一个元素, 进行查找. 再就是注意每次find时的规律, 事实上就是查找的顺序起始
这里我把每一个url都保存在一个数组中, 检索完毕后, 对数组里的url进行下载.
- #crawler甘肃农业大学新闻网板块的->学校新闻.
- #http://news.gsau.edu.cn/tzgg1/xxxw33.htm
- #<a class="c43092" href="../info/1037/30567.htm" target="_blank" title="双联行动水泉乡克那村工作组赴联系村开展精准扶贫相关工作">双联行动水泉乡克那村工作组赴联系村开展精准扶贫相关工作</a>
- #coding:utf-8
- import urllib
- import time
- import stat, os
- pageSize = 23
- articleList = urllib.urlopen('http://news.gsau.edu.cn/tzgg1/xxxw33.htm').read()
- urlList = [' ']*pageSize
- #锁定class="c43092"
- hrefClass = articleList.find('class="c43092"')
- hrefBeg = articleList.find(r'href=', hrefClass)
- hrefEnd = articleList.find(r'.htm', hrefBeg)
- href=articleList[hrefBeg+6: hrefEnd+4][3:]
- print href
- #url = 'http://news.gsau.edu.cn/' + href
- #print 'url: '+url
- i = 0
- while href!=-1 and i<pageSize:
- urlList[i] = 'http://news.gsau.edu.cn/' + href
- hrefClass = articleList.find('class="c43092"', hrefEnd)
- hrefBeg = articleList.find(r'href=', hrefClass)
- hrefEnd = articleList.find(r'.htm', hrefBeg)
- href=articleList[hrefBeg+6: hrefEnd+4][3:]
- print urlList[i]
- i = i+1
- else:
- print r'本页所有URL已爬完!!!'
- #将本页每一篇新闻下载到本地(已新闻标题文件名称存储)
- #title: <HTML><HEAD><TITLE>酒泉市市长都伟来校对接校地合作事宜-新闻网</TITLE>
- j = 0
- while j<pageSize:
- content = urllib.urlopen(urlList[j]).read()
- titleBeg = content.find(r'<TITLE>')
- titleEnd = content.find(r'</TITLE>', titleBeg)
- title = content[titleBeg+7: titleEnd]
- print title
- print urlList[j]+r'正在下载...'
- time.sleep(1)
- open(r'GsauNews' + os.path.sep + title.decode('utf-8').encode('gbk')+'.html', 'w+').write(content)
- j = j + 1
- else:
- print r'当页所有url完成下载!'
第三步: 爬取N个页面的全部新闻
这里要爬取N个页面, 首先就要分析你要爬取的是最新的, 而不能是固定的某几页
所以要分析下分页的数据, 正好主页最以下也给出了分页数据, 直接用它!
看下近期几页的url:
- #http://news.gsau.edu.cn/tzgg1/xxxw33.htm 第一页
- #http://news.gsau.edu.cn/tzgg1/xxxw33/221.htm 第二页
- #http://news.gsau.edu.cn/tzgg1/xxxw33/220.htm 第三页
- #http://news.gsau.edu.cn/tzgg1/xxxw33/219.htm 第四页
对照分页数据, 非常easy发现规律, 就是: fenyeCount-pageNo+1
这里非常烦的一点是不知道为什么, 除了第一页以外的其它页, 都会有不是本页而是前一页的一部分网页数据会掺进来. 导致我找了半天
做了非常多推断才检索出来
- #crawler甘肃农业大学新闻网板块的->学校新闻.
- #coding:utf-8
- import urllib
- import time
- import stat, os
- pageCount = 4
- pageSize = 23
- pageNo = 1
- urlList = [' ']*pageSize*pageCount
- #分析分页的网页元素
- #<td width="1%" align="left" id="fanye43092" nowrap="">共5084条 1/222 </td>
- indexContent = urllib.urlopen('http://news.gsau.edu.cn/tzgg1/xxxw33.htm').read()
- fenyeId = indexContent.find('id="fanye43092"') #这里锁定分页的id进行查找
- fenyeBeg = indexContent.find('1/', fenyeId)
- fenyeEnd = indexContent.find(' ', fenyeBeg)
- fenyeCount = int(indexContent[fenyeBeg+2: fenyeEnd])
- i = 0
- while pageNo <= pageCount:
- if pageNo==1:
- articleUrl = 'http://news.gsau.edu.cn/tzgg1/xxxw33.htm'
- else:
- articleUrl = 'http://news.gsau.edu.cn/tzgg1/xxxw33/'+ str(fenyeCount-pageNo+1) + '.htm'
- print r'--------共爬取'+ str(pageCount) + '页 当前第' + str(pageNo) + '页 URL:' + articleUrl
- articleList = urllib.urlopen(articleUrl).read()
- while i<pageSize*pageNo:
- if pageNo == 1:
- #i = 0,23,46...时,从头找, 其余从上一个url结束位置开找
- if i == pageSize*(pageNo-1):
- hrefId = articleList.find('id="line43092_0"')
- else:
- hrefId = articleList.find('class="c43092"', hrefEnd)
- else:
- if i == pageSize*(pageNo-1):
- hrefId = articleList.find('id="lineimg43092_16"')
- else:
- hrefId = articleList.find('class="c43092"', hrefEnd)
- hrefBeg = articleList.find(r'href=', hrefId)
- hrefEnd = articleList.find(r'.htm', hrefBeg)
- if pageNo == 1:
- href=articleList[hrefBeg+6: hrefEnd+4][3:]
- else:
- href=articleList[hrefBeg+6: hrefEnd+4][6:]
- urlList[i] = 'http://news.gsau.edu.cn/' + href
- print urlList[i]
- i = i+1
- else:
- print r'========第'+str(pageNo)+'页url提取完毕!!!'
- pageNo = pageNo + 1
- print r'============全部url提取完毕!!!============'+'\n'*3
- print r'==========開始下载到本地==========='
- j = 0
- while j < pageCount * pageSize:
- content = urllib.urlopen(urlList[j]).read()
- titleBeg = content.find(r'<TITLE>')
- titleEnd = content.find(r'</TITLE>', titleBeg)
- title = content[titleBeg+7: titleEnd]
- print title
- print urlList[j]+r'正在下载...'+'\n'
- time.sleep(1)
- open(r'GsauNews' + os.path.sep + title.decode('utf-8').encode('gbk')+'.html', 'w+').write(content)
- j = j + 1
- else:
- print r'下载完毕, 共下载'+str(pageCount)+'页, '+str(pageCount*pageSize)+'篇新闻'
这就爬完了....
看下爬完的效果
- ==================== RESTART: D:\python\CSDNCrawler03.py ====================
- --------共爬取4页 当前第1页 URL:http://news.gsau.edu.cn/tzgg1/xxxw33.htm
- http://news.gsau.edu.cn/info/1037/30596.htm
- http://news.gsau.edu.cn/info/1037/30595.htm
- http://news.gsau.edu.cn/info/1037/30593.htm
- http://news.gsau.edu.cn/info/1037/30591.htm
- http://news.gsau.edu.cn/info/1037/30584.htm
- http://news.gsau.edu.cn/info/1037/30583.htm
- http://news.gsau.edu.cn/info/1037/30580.htm
- http://news.gsau.edu.cn/info/1037/30577.htm
- http://news.gsau.edu.cn/info/1037/30574.htm
- http://news.gsau.edu.cn/info/1037/30573.htm
- http://news.gsau.edu.cn/info/1037/30571.htm
- http://news.gsau.edu.cn/info/1037/30569.htm
- http://news.gsau.edu.cn/info/1037/30567.htm
- http://news.gsau.edu.cn/info/1037/30566.htm
- http://news.gsau.edu.cn/info/1037/30565.htm
- http://news.gsau.edu.cn/info/1037/30559.htm
- http://news.gsau.edu.cn/info/1037/30558.htm
- http://news.gsau.edu.cn/info/1037/30557.htm
- http://news.gsau.edu.cn/info/1037/30555.htm
- http://news.gsau.edu.cn/info/1037/30554.htm
- http://news.gsau.edu.cn/info/1037/30546.htm
- http://news.gsau.edu.cn/info/1037/30542.htm
- http://news.gsau.edu.cn/info/1037/30540.htm
- ========第1页url提取完毕!!!
- --------共爬取4页 当前第2页 URL:http://news.gsau.edu.cn/tzgg1/xxxw33/221.htm
- http://news.gsau.edu.cn/info/1037/30536.htm
- http://news.gsau.edu.cn/info/1037/30534.htm
- http://news.gsau.edu.cn/info/1037/30528.htm
- http://news.gsau.edu.cn/info/1037/30525.htm
- http://news.gsau.edu.cn/info/1037/30527.htm
- http://news.gsau.edu.cn/info/1037/30524.htm
- http://news.gsau.edu.cn/info/1037/30520.htm
- http://news.gsau.edu.cn/info/1037/30519.htm
- http://news.gsau.edu.cn/info/1037/30515.htm
- http://news.gsau.edu.cn/info/1037/30508.htm
- http://news.gsau.edu.cn/info/1037/30507.htm
- http://news.gsau.edu.cn/info/1037/30506.htm
- http://news.gsau.edu.cn/info/1037/30505.htm
- http://news.gsau.edu.cn/info/1037/30501.htm
- http://news.gsau.edu.cn/info/1037/30498.htm
- http://news.gsau.edu.cn/info/1037/30495.htm
- http://news.gsau.edu.cn/info/1037/30493.htm
- http://news.gsau.edu.cn/info/1037/30482.htm
- http://news.gsau.edu.cn/info/1037/30480.htm
- http://news.gsau.edu.cn/info/1037/30472.htm
- http://news.gsau.edu.cn/info/1037/30471.htm
- http://news.gsau.edu.cn/info/1037/30470.htm
- http://news.gsau.edu.cn/info/1037/30469.htm
- ========第2页url提取完毕!!!
- --------共爬取4页 当前第3页 URL:http://news.gsau.edu.cn/tzgg1/xxxw33/220.htm
- http://news.gsau.edu.cn/info/1037/30468.htm
- http://news.gsau.edu.cn/info/1037/30467.htm
- http://news.gsau.edu.cn/info/1037/30466.htm
- http://news.gsau.edu.cn/info/1037/30465.htm
- http://news.gsau.edu.cn/info/1037/30461.htm
- http://news.gsau.edu.cn/info/1037/30457.htm
- http://news.gsau.edu.cn/info/1037/30452.htm
- http://news.gsau.edu.cn/info/1037/30450.htm
- http://news.gsau.edu.cn/info/1037/30449.htm
- http://news.gsau.edu.cn/info/1037/30441.htm
- http://news.gsau.edu.cn/info/1037/30437.htm
- http://news.gsau.edu.cn/info/1037/30429.htm
- http://news.gsau.edu.cn/info/1037/30422.htm
- http://news.gsau.edu.cn/info/1037/30408.htm
- http://news.gsau.edu.cn/info/1037/30397.htm
- http://news.gsau.edu.cn/info/1037/30396.htm
- http://news.gsau.edu.cn/info/1037/30394.htm
- http://news.gsau.edu.cn/info/1037/30392.htm
- http://news.gsau.edu.cn/info/1037/30390.htm
- http://news.gsau.edu.cn/info/1037/30386.htm
- http://news.gsau.edu.cn/info/1037/30385.htm
- http://news.gsau.edu.cn/info/1037/30376.htm
- http://news.gsau.edu.cn/info/1037/30374.htm
- ========第3页url提取完毕!!!
- --------共爬取4页 当前第4页 URL:http://news.gsau.edu.cn/tzgg1/xxxw33/219.htm
- http://news.gsau.edu.cn/info/1037/30370.htm
- http://news.gsau.edu.cn/info/1037/30369.htm
- http://news.gsau.edu.cn/info/1037/30355.htm
- http://news.gsau.edu.cn/info/1037/30345.htm
- http://news.gsau.edu.cn/info/1037/30343.htm
- http://news.gsau.edu.cn/info/1037/30342.htm
- http://news.gsau.edu.cn/info/1037/30340.htm
- http://news.gsau.edu.cn/info/1037/30339.htm
- http://news.gsau.edu.cn/info/1037/30335.htm
- http://news.gsau.edu.cn/info/1037/30333.htm
- http://news.gsau.edu.cn/info/1037/30331.htm
- http://news.gsau.edu.cn/info/1037/30324.htm
- http://news.gsau.edu.cn/info/1037/30312.htm
- http://news.gsau.edu.cn/info/1037/30311.htm
- http://news.gsau.edu.cn/info/1037/30302.htm
- http://news.gsau.edu.cn/info/1037/30301.htm
- http://news.gsau.edu.cn/info/1037/30298.htm
- http://news.gsau.edu.cn/info/1037/30294.htm
- http://news.gsau.edu.cn/info/1037/30293.htm
- http://news.gsau.edu.cn/info/1037/30289.htm
- http://news.gsau.edu.cn/info/1037/30287.htm
- http://news.gsau.edu.cn/info/1037/30286.htm
- http://news.gsau.edu.cn/info/1037/30279.htm
- ========第4页url提取完毕!!!
- ============全部url提取完毕!!!============
- ==========開始下载到本地===========
- 甘肃爽口源生态科技股份有限公司来校洽谈校企合作-新闻网
- http://news.gsau.edu.cn/info/1037/30596.htm正在下载...
- 我校第二届辅导员职业能力大赛圆满落幕-新闻网
- http://news.gsau.edu.cn/info/1037/30595.htm正在下载...
- 双联行动周家山村工作组开展精准扶贫工作-新闻网
- http://news.gsau.edu.cn/info/1037/30593.htm正在下载...
- 新疆生产建设兵团来校举办专场校园招聘会-新闻网
- http://news.gsau.edu.cn/info/1037/30591.htm正在下载...
- 【图片新闻】桃李吐新叶 羲园醉春风-新闻网
- http://news.gsau.edu.cn/info/1037/30584.htm正在下载...
- 甘农师生爱心救助白血病学生张渭-新闻网
- http://news.gsau.edu.cn/info/1037/30583.htm正在下载...
- 副校长赵兴绪带队赴新庄村开展精准扶贫相关工作-新闻网
- http://news.gsau.edu.cn/info/1037/30580.htm正在下载...
- 双联行动红庄村工作组开展精准扶贫工作-新闻网
- http://news.gsau.edu.cn/info/1037/30577.htm正在下载...
- 校长吴建民带队赴广河县开展精准扶贫和双联工作-新闻网
- http://news.gsau.edu.cn/info/1037/30574.htm正在下载...
- 动物医学院赴园子村开展精准扶贫相关工作-新闻网
- http://news.gsau.edu.cn/info/1037/30573.htm正在下载...
- ..........
一分多钟爬了90个网页.
当然代码还能够优化好多. 可是我Python基础实在薄弱.还是去恶补
还能够继续升级, 用正则, 匹配自己想要的内容,而不是像这样泛泛全保存下来.
能够在以后的学习中继续改进. 爬些更有意思的东西
做这个样例仅仅是为了看看, 初学Python能为我做什么.
Python写爬虫-爬甘农大学校新闻的更多相关文章
- Python写爬虫爬妹子
最近学完Python,写了几个爬虫练练手,网上的教程有很多,但是有的已经不能爬了,主要是网站经常改,可是爬虫还是有通用的思路的,即下载数据.解析数据.保存数据.下面一一来讲. 1.下载数据 首先打 ...
- 用Python写爬虫爬取58同城二手交易数据
爬了14W数据,存入Mongodb,用Charts库展示统计结果,这里展示一个示意 模块1 获取分类url列表 from bs4 import BeautifulSoup import request ...
- 利用Python网络爬虫爬取学校官网十条标题
利用Python网络爬虫爬取学校官网十条标题 案例代码: # __author : "J" # date : 2018-03-06 # 导入需要用到的库文件 import urll ...
- python写爬虫时的编码问题解决方案
在使用Python写爬虫的时候,常常会遇到各种令人抓狂的编码错误问题.下面给出一些简单的解决编码错误问题的思路,希望对大家有所帮助. 首先,打开你要爬取的网站,右击查看源码,查看它指定的编码是什么,如 ...
- 如何利用Python网络爬虫爬取微信朋友圈动态--附代码(下)
前天给大家分享了如何利用Python网络爬虫爬取微信朋友圈数据的上篇(理论篇),今天给大家分享一下代码实现(实战篇),接着上篇往下继续深入. 一.代码实现 1.修改Scrapy项目中的items.py ...
- 《用Python写爬虫》学习笔记(一)
注:纯文本内容,代码独立另写,属于本人学习总结,无任何商业用途,在此分享,如有错误,还望指教. 1.为什么需要爬虫? 答:目前网络API未完全放开,所以需要网络爬虫知识. 2.爬虫的合法性? 答:爬虫 ...
- 怎么用Python写爬虫抓取网页数据
机器学习首先面临的一个问题就是准备数据,数据的来源大概有这么几种:公司积累数据,购买,交换,政府机构及企业公开的数据,通过爬虫从网上抓取.本篇介绍怎么写一个爬虫从网上抓取公开的数据. 很多语言都可以写 ...
- 开发记录_自学Python写爬虫程序爬取csdn个人博客信息
每天刷开csdn的博客,看到一整个页面,其实对我而言,我只想看看访问量有没有上涨而已... 于是萌生了一个想法: 想写一个爬虫程序把csdn博客上边的访问量和评论数都爬下来. 打算通过网络各种搜集资料 ...
- 定向爬虫之爬一爬各个学校新闻的认识(【1】对Url的认识)
昨天早上,我习惯性的打开博客园,看一看别人的写的博客.突然想起,自己好像没有写过什么博客,所以就心血来潮,把我现在做得事情写出来, 这也是对我目前的学习的一种总结.望大神指点.... 对于一间学校的新 ...
随机推荐
- Perl初学笔记
标量数据 标量:数字和字符串. 数字:Perl不存在整形,全部是double类型.整形常量会被自动转换为浮点型. Perl数字字面量(直接量):+-和小数点是非必须的,e代表10的多少次方.例如:-1 ...
- Reference Counting GC (Part one)
目录 引用计数法 计数器值的增减 new_obj()和update_ptr()函数 new_obj()生成对象 update_ptr()更新指针ptr,对计数器进行增减 优点 可即可回收垃圾 最大暂停 ...
- 03012_预处理对象executeQuery方法(实现数据库的查询)
1.概述 (1)通过预处理对象的executeQuery方法,完成记录的select语句的执行: (2)操作格式统一如下: ①注册驱动: ②获取连接: ③获取预处理对象: ④SQL语句占位符设置实际参 ...
- Saltstack的API接口与调用方式
saltstack看起来是成为一个大规模自己主动化运维和云计算管理的一个框架,类似于SDK,并非像puppet仅仅成为一个工具.基于良好设计的API和清楚的思路,让salt的二次开发变得非常easy ...
- [ES6] The Iterator Protocol
The iterator protocol is used to define a standard way that an object produces a sequence of values. ...
- Codeforces Round #262 (Div. 2) 题解
A. Vasya and Socks time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- Linux下Oracle的sqlplus中上下左右退格键无法使用
一.配置yum源并安装readline* 配置本地yum 1.挂载光盘 mount /dev/cdrom /mnt/media 2,新建本地yun源的配置文件 vi /etc/yum.repos.d/ ...
- POJ 2104 K-th Number 静态主席树(裸
题目链接:点击打开链接 题意: 给定n长的序列.q个询问 以下n个数字给出序列 每一个询问[l, r] k ,输出该区间中第k大的数 先建一个n个节点的空树.然后每次从后往前新建一棵树,依附原来的空树 ...
- [UnityUI]循环滑动列表
效果图: 使用的是UGUI和DOTween 当中比較关键的是循环滑动和层次排序: 1.循环滑动:这里先如果显示五张图片.分别标记为0,1,2,3,4,那么当向左滑动时,序列就变为1,2,3,4,0,这 ...
- 关于MySQL utf8mb4 字符集中字符串长度的问题
MySQL之前推出的utf8字符集中,一个汉字占3个字节,新的utf8mb4字符集中一个汉字占4个字节. 那么我们平时建表的时候输入的varchar=16这种,到底指的是字符长度还是字节长度? 如果是 ...