• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python实现的下载8000首儿歌的代码分享

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

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python实现人脸识别,python实现冒泡排序,python实现神经网络,python实现svm,python实现决策树等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

下载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)
</div>

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

</div>

您可能想查找下面的文章:

  • Python实现视频下载功能
  • python实现读取并显示图片的两种方法
  • python实现获取Ip归属地等信息
  • python实现获取Ip归属地等信息
  • 基于Python实现一个简单的银行转账操作
  • 基于Python实现一个简单的银行转账操作
  • python实现识别相似图片小结
  • python实现识别相似图片小结
  • Python实现模拟登录及表单提交的方法
  • Python实现批量将word转html并将html内容发布至网站的方法

相关文章

  • Python的requests网络编程包使用教程
  • 打开电脑上的QQ的python代码
  • 教你用python3根据关键词爬取百度百科的内容
  • Python环境变量设置方法
  • python安装PIL模块时Unable to find vcvarsall.bat错误的解决方法
  • Python监控主机是否存活并以邮件报警
  • 利用python代码写的12306订票代码
  • Python最基本的输入输出详解
  • 使用IronPython把Python脚本集成到.NET程序中的教程
  • Python中使用select模块实现非阻塞的IO

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 跟老齐学Python之使用Python操作数据库(1)
    • python中global与nonlocal比较
    • 用Python代码来绘制彭罗斯点阵的教程
    • windows 10下安装搭建django1.10.3和Apache2.4的方法
    • Python实现向QQ群成员自动发邮件的方法
    • python实现将元祖转换成数组的方法
    • 详解在Python程序中使用Cookie的教程
    • python中合并两个文本文件并按照姓名首字母排序的例子
    • python实现从字典中删除元素的方法
    • Python函数嵌套实例

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有