Python实现的下载8000首儿歌的代码分享
作者:junjie 发布时间:2021-02-03 05:41:51
下载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)
有需要的可以参考继续修改。
猜你喜欢
- 前言用阻塞 API 写同步代码最简单,但一个线程同一时间只能处理一个请求,有限的线程数导致无法实现万级别的并发连接,过多的线程切换也抢走了
- 1. 题目编写程序, 4名牌手打牌,计算机随机将52张牌(不含大小鬼)发给4名牌手,在屏幕上显示每位牌手的牌。提示:设计出3个类:Card类
- 列表(List)是你使用Python过程中接触最为频繁的数据结构,也是功能最为强大的几种数据结构之一。Python列表非常的万能且蕴含着许多
- 环境:OS:Red Hat Enterprise Linux AS release 4 (Nahant)DB:Oracle Database
- 描述super() 函数用于调用下一个父类(超类)并返回该父类实例的方法。super 是用来解决多重继承问题的,直接用类名调用父类方法在使用
- 本文实例讲述了python实现线程池的方法。分享给大家供大家参考。具体如下:原理:建立一个任务队列,然多个线程都从这个任务队列中取出任务然后
- 装饰器简介:装饰器(decorator)是一种高级Python语法。装饰器可以对一个函数、方法或者类进行加工。在Python中,我们有多种方
- 前言因为自已平时会把一个常用到逻辑写成一个工具python脚本,像关于时间字符串处理,像关于路径和文件夹遍历什么的工具。每一次新建一个项目的
- CSRF(Cross Site Request Forgery, 跨站域请求伪造)是一种网络的×××方式。我的理解是,比如你访问过招商银行的
- 这篇文章主要介绍了python常用排序算法的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友
- 1.首先分析要做的项目的结构,整理出关系图2.运行 python manage.py startapp XXX 创建一个app3.根据关系图
- bootstrap自带的响应式导航栏是向下滑动的,有时满足不了个性化的需求,需要做一个类似于android drawerLayout 侧滑的
- 刚才好无聊,突然想起来之前做一个课表的点子,于是百度了起来。刚开始,我是这样想的:在写微信墙的时候,用到了urllib2【两行代码抓网页】,
- Linux Journal 发表了一篇优化 Oracle 数据库的文章,感觉十分的有用。简要介绍其摘要和大家共同分享 Linux 在企业级数
- 我们在用Python进行机器学习建模项目的时候,每个人都会有自己的一套项目文件管理的习惯,我自己也有一套方法,是自己曾经踩过的坑总结出来的,
- 下面是模板的一般形式,显示了指定 SQL 查询和 XPath 查询的方式: <ROOT xmlns:sql="ur
- Python等工具确实是不错的工具,但是有时候不管是基础的Python还是Python的软件包都让我觉得对中文不是很亲近。时不时地遇到一点问
- Enum 是个类所以基本的类操作都可以用也就是我们可以添加自己的方法class Mood(Enum): FUNKY
- python对函数库的引用方式1、import <库名> 例如:import turtle 如果需要使用库函数中的函数,需要使用
- 步骤如下:1.图片灰化;2.中值滤波 去噪3.求图片的光影(自动光学检测)4.除法去光影5.阈值操作6.实现了三种目标检测方法主要分两种连通