Python实现的下载8000首儿歌的代码分享


Posted in Python onNovember 21, 2014

下载8000首儿歌的python的代码:

#-*- coding: UTF-8 -*-
from pyquery import PyQuery as py

from lxml import etree

import urllib

import re

import os

import sys

import logging
def format(filename):

    tuple=(' ',''','\'')

    for char in tuple:

        if (filename.find(char)!=-1):

            filename=filename.replace(char,"_")

    return filename
def download_mp3(mp3_url, filename,dir):       

    f = dir+"\\"+filename

    if os.path.exists(f):

      logger.debug(f+" is existed.")

      return

      

    try:

        open(f, 'wb').write(urllib.urlopen(mp3_url).read())

        logger.debug(  filename + ' is downloaded.')

    except:

        logger.debug( filename + ' is not downloaded.')
        

def download_all_mp3(start,end,dir,logger):

  for x in range(start,end):

    try:

        url = "http://www.youban.com/mp3-d" + str(x) + ".html"

        logger.debug(str(x) + ": "+url)

        doc = py(url=url)

        e = doc('.mp3downloadbox')

        if e is None or e == '': 

          logger.debug(url+" is not existed.")

          return

          

        e = unicode(e)

        #logger.debug( e)

        regex = re.compile(ur".*<h1>(.*)</h1>.*downloadboxlist.*?<a.*?\"(.*?)\"",re.UNICODE|re.S)

        m = regex.search(e)

        if m is not None:

          title = m.group(1).strip()

          title2 = str(x)+"_"+title + ".mp3" 

          #title2 = re.sub(' ','_',title2)

          title2 = format(title2)

          link = m.group(2)

          #logger.debug( "title:" + title + " link:" + link)

          if link == '' or title == '':

            logger.debug(url + " is not useful")

            continue

          logger.debug(str(x)+": "+link)

          download_mp3(link,title2,dir)

    except:

        logger.debug(url+" met exception.")

        continue

     
      

if __name__ == "__main__":

    dir_root = "e:\\song"

    if sys.argv[3] != '': dir_root=sys.argv[3]

    

    start,end = 1,8000

    if sys.argv[1] >= 0 and sys.argv[2]>=0:

      start,end = int(sys.argv[1]),int(sys.argv[2])

      print ("Download from %s to %s.\n" % (start,end))      

    

    dir = dir_root + "\\"+str(start)+"-"+str(end)

    if not os.path.exists(dir): 

      os.mkdir(dir)     

    print "Download to " + dir + ".\n"

    

    logger = logging.getLogger("simple")

    logger.setLevel(logging.DEBUG)  

    fh = logging.FileHandler(dir+"\\"+"download.log")

    ch = logging.StreamHandler()

    formatter = logging.Formatter("%(message)s")

    ch.setFormatter(formatter)

    fh.setFormatter(formatter) 

    logger.addHandler(ch)

    logger.addHandler(fh)

    download_all_mp3(start,end,dir,logger)

有需要的可以参考继续修改。

Python 相关文章推荐
python 获取本机ip地址的两个方法
Feb 25 Python
python实现带错误处理功能的远程文件读取方法
Apr 29 Python
python使用mailbox打印电子邮件的方法
Apr 30 Python
python实现的简单抽奖系统实例
May 22 Python
Python中特殊函数集锦
Jul 27 Python
python实现远程通过网络邮件控制计算机重启或关机
Feb 22 Python
用PyInstaller把Python代码打包成单个独立的exe可执行文件
May 26 Python
详解用pyecharts Geo实现动态数据热力图城市找不到问题解决
Jun 26 Python
Python time库基本使用方法分析
Dec 13 Python
Python中X[:,0]和X[:,1]的用法
May 10 Python
使用Python开发贪吃蛇游戏 SnakeGame
Apr 30 Python
python通过新建环境安装tfx的问题
May 20 Python
Python常用模块介绍
Nov 21 #Python
Python使用py2exe打包程序介绍
Nov 20 #Python
Python实现的tab文件操作类分享
Nov 20 #Python
Python实现的ini文件操作类分享
Nov 20 #Python
Python中列表、字典、元组、集合数据结构整理
Nov 20 #Python
python有证书的加密解密实现方法
Nov 19 #Python
Python采用socket模拟TCP通讯的实现方法
Nov 19 #Python
You might like
PHP Mysql编程之高级技巧
2008/08/27 PHP
php cURL和Rolling cURL并发方式比较
2013/10/30 PHP
php实现的SESSION类
2014/12/02 PHP
php实现遍历多维数组的方法
2015/11/25 PHP
PHP文件缓存smarty模板应用实例分析
2016/02/26 PHP
yii2中LinkPager增加总页数和总记录数的实例
2017/08/28 PHP
Autocomplete Textbox Example javascript实现自动完成成功
2007/08/17 Javascript
jQuery实现多按钮单击变色
2014/11/27 Javascript
JavaScript实现数字数组按照倒序排列的方法
2015/04/06 Javascript
FullCalendar日历插件应用之数据展现(一)
2015/12/23 Javascript
javascript自动恢复文本框点击清除后的默认文本
2016/01/12 Javascript
基于js实现的限制文本框只可以输入数字
2016/12/05 Javascript
基于es6三点运算符的使用方法(实例讲解)
2017/10/12 Javascript
layui表格checkbox选择全选样式及功能的实例
2018/03/07 Javascript
JQuery中的常用事件、对象属性与使用方法分析
2019/12/23 jQuery
[02:28]DOTA2英雄基础教程 狼人
2013/12/23 DOTA
Python中给List添加元素的4种方法分享
2014/11/28 Python
Python外星人入侵游戏编程完整版
2020/03/30 Python
对PyQt5的输入对话框使用(QInputDialog)详解
2019/06/25 Python
Python代码实现http/https代理服务器的脚本
2019/08/12 Python
python nmap实现端口扫描器教程
2020/05/28 Python
PyQt5实现登录页面
2020/05/30 Python
python 基于opencv 实现一个鼠标绘图小程序
2020/12/11 Python
iHerb香港:维生素、补充剂和天然保健品
2017/08/01 全球购物
英国豪华针织品牌John Smedley的在线销售商:The Outlet by John Smedley
2018/04/08 全球购物
中国一家专注拼团的社交购物网站:拼多多
2018/06/13 全球购物
性能服装:HYLETE
2018/08/14 全球购物
会计自荐书
2013/12/02 职场文书
保险公司年会主持词
2014/03/22 职场文书
工程承包协议书
2014/04/22 职场文书
银行主办会计岗位职责
2014/08/13 职场文书
水利局群众路线专题民主生活会发言材料
2014/09/21 职场文书
2014年环境卫生工作总结
2014/11/24 职场文书
现场施工员岗位职责
2015/04/11 职场文书
公司清洁工岗位职责
2015/04/15 职场文书
实习员工转正的评语汇总,以备不时之需
2019/12/17 职场文书