码迷,mamicode.com
首页 > 编程语言 > 详细

python 批量下载 spring 的 xsd

时间:2017-07-14 10:03:46      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:sts   blog   超时   pen   pil   esb   时间   sch   style   

#coding=utf-8

import os
import urllib
import urllib2
import re
from bs4 import BeautifulSoup
# 利用 urllib.urlretrieve() 函数进行下载。非常方便

import socket
#超时时间
socket.setdefaulttimeout(5)

# 一个空文件夹
basedir=r"E:\spring".decode(utf-8)
os.chdir(basedir)
host="http://www.springframework.org"

def getFolderName(url):
    pattern = re.compile(r.*/(.*))
    match = pattern.match(url)
    # 返回最后一个 / 后面的字符
    if match:
        return match.group(1)


# 存储spring 的集合
list = [http://www.springframework.org/schema/beans,
        http://www.springframework.org/schema/aop,
        http://www.springframework.org/schema/mvc,
        http://www.springframework.org/schema/p,
        http://www.springframework.org/schema/context,
        http://www.springframework.org/schema/tx
        ]

# 获取页面中的 文件名称
def getFilesByUrl(url):
    #返回列表
    list=[]
    try:
        html = urllib2.urlopen(url)
    except Exception as err:
        print err
    soup = BeautifulSoup(html,"lxml")
    for link in soup.find_all(a):
        fileName = link[href]
        if not fileName.startswith(/) and (fileName.endswith(xsd) or fileName.endswith(/)):
            list.append(fileName)
    return list

list = getFilesByUrl("http://www.springframework.org/schema")
print list
for url in list:
    print url[:-1]
    url = "http://www.springframework.org/schema/"+url[:-1]
    folder = getFolderName(url)
    print folder
    if not os.path.exists(folder):
        os.mkdir(folder)
    files = getFilesByUrl(url)
    print files
    for fileName in files:
        try:
            print url+"/"+fileName
            urllib.urlretrieve(url+"/"+fileName,folder+"/"+fileName)
        except Exception as err:
            print err

 

python 批量下载 spring 的 xsd

标签:sts   blog   超时   pen   pil   esb   时间   sch   style   

原文地址:http://www.cnblogs.com/whm-blog/p/7168224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!