zoukankan      html  css  js  c++  java
  • 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的确是一门值得学习的课程.


    关于网络爬虫:

    这个东西我也也不懂... 随便抓个解释, 淡淡的理解下
    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更常常的称为网页追逐者)。是一种依照一定的规则。自己主动地抓取万维网信息的程序或者脚本。另外一些不常使用的名字还有蚂蚁、自己主动索引、模拟程序或者蠕虫。 
    真正的复杂而强大的爬虫有非常多爬行算法和策略.
    我写的这个样例简直是简之又简. 


    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提取完毕!!!============'+'
    '*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'正在下载...'+'
    '
        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:pythonCSDNCrawler03.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能为我做什么.








  • 相关阅读:
    js选项卡
    js 逻辑运算符
    git 标签管理
    git多人协作
    git 分支强制删除
    git bug修复
    DOS命令编译JAVA程序
    JDK的安装与配置
    我在linux的第一个C程序
    看我如何在控制台一行显示几万字符。
  • 原文地址:https://www.cnblogs.com/llguanli/p/7136435.html
Copyright © 2011-2022 走看看