Python下载示例

24 次查看 0 条评论
#!/usr/bin/python
# -*- coding: UTF-8 -*-

## 获取真实文件名
def getFileName(url):
    import urllib
    import os
    r = urllib.urlopen(url)
    if r.info().has_key('Content-Disposition'):
        fileName = r.info()['Content-Disposition'].split('filename=')[1]
        fileName = fileName.replace('"', '').replace("'", "")
    elif r.url != url:
        from os.path import basename
        from urlparse import urlsplit
        fileName = basename(urlsplit(r.url)[2])
    else:
        fileName = os.path.basename(url)
    return fileName

## 将保存路径写入MySQL数据库
def writePathToMySQL(url,path):
    import MySQLdb
    db = MySQLdb.connect("localhost","root","123456","test",charset='utf8' )
    cursor = db.cursor()
    #sql = "update download set path = '%s' where url = '%s'" %(path,url)
    sql = "insert into download(url,path) values ('%s','%s')" %(url,path)
    print "SQL:" + sql
    try:
        cursor.execute(sql)
        db.commit()
    except:
        db.rollback()
        print "写入数据库失败!" 
    db.close()
#writePathToMySQL('http://0903.beauty698.com/apk/bole_mky002.apk','/root/download/bole_mky002.apk')

## 导入原始数据到数据库
def insertRawData(url):
    import MySQLdb
    db = MySQLdb.connect("localhost","root","123456","test",charset='utf8' )
    cursor = db.cursor()
    #sql = "update pkg_raw_info set path = '%s' where url = '%s'" %(path,url)
    sql = "insert into pkg_raw_info(url) values ('%s')" %(url)
    print "SQL:" + sql
    try:
        cursor.execute(sql)
        db.commit()
    except:
        db.rollback()
        print "写入数据库失败!" 
    db.close()

## 执行下载
def download(url):
    import urllib
    import urllib2
    import os
    print "URL: " + url 
    savePath = os.path.abspath('.') + "/download/" + str(getFileName(url))
    print "Save Path:" + savePath
    result = urllib.urlretrieve(url,filename=savePath,reporthook=None) 
    ## 调用函数把结果写入数据库
    print "保存路径写入数据库。。。"
    writePathToMySQL(url,savePath)
    print "##################################################"

## 读取文本
def readFile(path):
    fd = open(path)
    i = 1
    for url in fd:
        #过滤去掉换行
        url = url.strip('\n')
        #print url
        print "正在下载第" + str(i) + "个链接资源。。。"
        download(url)
        i = i+1
    print "所有下载任务完成!"

## 开始入口
readFile('/test.txt')

SQL:

DROP TABLE IF EXISTS `download`;
CREATE TABLE `download` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `url` varchar(255) NOT NULL,
  `path` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`,`url`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
Other none

暂无评论,快来抢沙发。