zoukankan      html  css  js  c++  java
  • 利用python脚本自动下载ICML会议接受的文章

          最近需要下载ICML会议2015年接受的最新的文章,但是到官网一看,那么多的文章,如果我一篇一篇点击下载的话,什么时候是个头呢?于是就想着用python脚本对文章的页面进行处理,得到相关文章的url,然后进行下载。

          通过观察ICML会议的Accepted Papers发现,其的结构还是比较整齐的,其中我们需要的信息的代码片段如下:

    <div class="paper">
        <p class="title">Approval Voting and Incentives in Crowdsourcing</p>
        <p class="details">
        <span class="authors">
                Nihar Shah,
            
                Dengyong Zhou,
            
                Yuval Peres
        </span>
        </p>
        <p class="links">
            [<a href="shaha15.html">abs</a>]
            [<a href="shaha15.pdf">pdf</a>]
            [<a href="shaha15-supp.pdf">supplementary</a>]
        </p>
    </div>

    只要我们提取到了title和具体文章的连接这件事计算完成了。

        提取html的相关的内容一般有两种方式:

    1.      对html文档进行解析
    2. 利用正则表达式进行内容匹配

        对html文档进行解析要比利用正则表达式进行内容匹配要慢,但是对于我的这个小的数据处理,速度不是首要的要求,最重要的是能够实现。所以就试着用了下HtmlPaper,但是这好像不是我要的,用起来比较困难,就转而使用python的正则表达式来进行匹配。为了匹配以上我们需要的内容,我写了如下的正则表达式,并对文章的标题和url进行了分组。

    <div.*?class="paper".*?>[sS]*?<p.*?class="title".*?>([sS]*?)</p>[sS]*?<a.*?href="(.*?.pdf)">pdf</a>[sS]*?</div>

       整个python脚本的流程是:

    1. 得到要处理的html文档
    2. 对文章的标题和url进行提取
    3. 对url的资源进行下载并保存为标题对应的pdf文档

    全部的代码如下:

    # -*- coding: utf-8 -*-  
    import urllib2
    import re
    
    def getDocument():
        url='http://jmlr.org/proceedings/papers/v37/'
        response=urllib2.urlopen(url)
        return response.read()
    
    
    def download(url,file):
        """
        download the file 
    
        @parameters
        url:the resource of the file 
        file:the name to save the file
        """
        f=urllib2.urlopen(url)
        with open(file+'.pdf','wb') as output:
            output.write(f.read())
    
    def  process(document):
        #print document
        p=re.compile('<div.*?class="paper".*?>[sS]*?<p.*?class="title".*?>([sS]*?)</p>[sS]*?<a.*?href="(.*?.pdf)">pdf</a>[sS]*?</div>',re.IGNORECASE)
        m=p.finditer(document)
        url='http://jmlr.org/proceedings/papers/v37/'
        for i in m:
            print 'title:',i.group(1)
            print 'url:',url+i.group(2)
            print 'downloading....'
            download(url+i.group(2),i.group(1))
    
    if __name__ == '__main__':
        process(getDocument())

       运行以上脚本:

    在对应为文件夹下,可以看到下载的papers:

     

    打开其中一篇,也能够正常显示:

    ps:唯一不足的是,我们可以看到有的文章是有补充的,但是在我写正则表达式的时候没有试验成功,也没有再深究,有知道的同学不吝赐教。因为是有的文章有,有的文章是没有的嘛,所以我想就是若存在则匹配,若不存在,则匹配不到,由于对正则表达式不是很熟悉,先到这里,以后找到解决方式的话再更新。没有技术难度,仅作日常记录。

  • 相关阅读:
    Sum Root to Leaf Numbers
    Sum Root to Leaf Numbers
    Sort Colors
    Partition List
    Binary Tree Inorder Traversal
    Binary Tree Postorder Traversal
    Remove Duplicates from Sorted List II
    Remove Duplicates from Sorted List
    Search a 2D Matrix
    leetcode221
  • 原文地址:https://www.cnblogs.com/Andy-wen/p/4854238.html
Copyright © 2011-2022 走看看