python : 批量下载R语言库包

时间:2014-10-12 03:04:47   收藏:0   阅读:418

soupR.py 代码如下

# -*- coding: cp936 -*-
import urllib
import urllib2
import os, re
from BeautifulSoup import BeautifulSoup

localDir = 'D:/test/'
URL = "http://cran.dataguru.cn/bin/windows/contrib/3.1/"
page = urllib2.urlopen(URL)
soup = BeautifulSoup(page)
page.close()

for a in soup.findAll('a'): 
    if a is not None:
        text = a.text.strip()
        #if re.search('map',text):
        if text.startswith('map'):
            href = a.get('href')
            if href.endswith('.zip'):
                #print href
                localFile = localDir + text
                if os.path.exists(localFile):
                    print localFile,'is exists'
                else:
                    try:
                        # 按照url进行下载文件,并保存到本地目录
                        urllib.urlretrieve(URL+href,localFile)
                        print href,'is download'
                    except Exception,e:
                        continue


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!