1.开发流程

Created with Raphaël 2.2.0

开始

寻找歌词API

寻找python相关资料

写代码

检查bug

结束

2.软件流程

Created with Raphaël 2.2.0

开始

弹窗选择文件

检查弹窗返回值,确定文件个数

开始循环

post数据到API接口

下载全部

检查是否循环完毕

结束

yes

no

3.开始

3.0 准备工作

新建.py文件,安装json,requests,tkinter,os
规范mp3文件名'歌名-歌手.mp3'

3.1寻找API接口

笔者在这里用了http://doc.gecimi.com/en/latest/ 这里的API歌词接口,从该开发文档来看,返回值为Json
例如"http://gecimi.com/api/lyric/海阔天空/Beyond"的返回值为:

{"code":0,"count":15,"result":[{"aid":1563419,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/166/16685/1668536.lrc","sid":1668536,"song":"海阔天空"},
{"aid":1567586,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/167/16739/1673997.lrc","sid":1673997,"song":"海阔天空"},
{"aid":1571906,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/167/16796/1679605.lrc","sid":1679605,"song":"海阔天空"},
{"aid":1573814,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/168/16819/1681961.lrc","sid":1681961,"song":"海阔天空"},
{"aid":1656038,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/179/17907/1790768.lrc","sid":1790768,"song":"海阔天空"},
{"aid":1718741,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/187/18757/1875769.lrc","sid":1875769,"song":"海阔天空"},
{"aid":2003267,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/226/22642/2264296.lrc","sid":2264296,"song":"海阔天空"},
{"aid":2020610,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/228/22889/2288967.lrc","sid":2288967,"song":"海阔天空"},{"aid":2051678,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/233/23323/2332322.lrc","sid":2332322,"song":"海阔天空"},
{"aid":2412704,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/283/28376/2837689.lrc","sid":2837689,"song":"海阔天空"},
{"aid":2607041,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/311/31116/3111659.lrc","sid":3111659,"song":"海阔天空"},
{"aid":2647055,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/316/31663/3166350.lrc","sid":3166350,"song":"海阔天空"},
{"aid":2657468,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/318/31803/3180339.lrc","sid":3180339,"song":"海阔天空"},
{"aid":3093833,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/377/37740/3774083.lrc","sid":3774083,"song":"海阔天空"},
{"aid":3161846,"artist_id":9208,"lrc":"http://s.gecimi.com/lrc/386/38612/3861244.lrc","sid":3861244,"song":"海阔天空"}]}

然后,我们用这段代码就可以把歌词解析出来

# coding: utf-8
import json
import requests def down(song, singer):
a = requests.get(
r"http://gecimi.com/api/lyric/"+song+"/"+singer)
a.raise_for_status()
a = a.text
res = json.loads(a)
if(res["count"] == 0):
return "null"
return res["result"][0]["lrc"] print(down("海阔天空", "beyond"))

其中,requests库的运用请参考我以前的blog传送门
json库的运用参考这里
至此,我们的抓取歌词代码就解决了

3.2 文件模块

3.2.1 选择文件弹窗

import tkinter.filedialog
def choice():
filename = tkinter.filedialog.askopenfilenames(
title=u'选择MP3文件', filetypes=[(u'MP3文件', '*.mp3')])
if filename != '':
return filename
else:
return "null" root = tkinter.Tk() # 创建一个Tkinter.Tk()实例
root.withdraw() # 将Tkinter.Tk()实例隐藏
c = choice()
for a in range(len(c)):
print(c[a])

用这段代码就可以实现,并且提取出每一项
参考这里菜鸟教程

3.2.2 提取文件名

通过这段代码可以提取

print(c[a][len(os.path.dirname(c[a]))+1:])

参考这里

3.2.2.1 提取歌名和歌手

info = name.split(".")[0].split("-")

这段可以实现

3.2.3 下载

参考这里
代码:

r = requests.get(down(info[0], info[1]))
with open(os.path.dirname(name)+"\\"+name.split(".")[0]+".lrc", "wb") as code:
code.write(r.content)

4.完成

全部代码:

# coding: utf-8
import os
import tkinter.filedialog
import json
import requests def down(song, singer): # 歌词API
a = requests.get(
r"http://gecimi.com/api/lyric/"+song+"/"+singer)
a.raise_for_status()
a = a.text
res = json.loads(a)
if(res["count"] == 0):
return "null"
return res["result"][0]["lrc"] def choice(): # 选择文件
filename = tkinter.filedialog.askopenfilenames(
title=u'选择MP3文件', filetypes=[(u'MP3文件', '*.mp3')])
if filename != '':
return filename
else:
return "null" # 开始
root = tkinter.Tk() # 创建一个Tkinter.Tk()实例
root.withdraw() # 将Tkinter.Tk()实例隐藏
c = choice()
if(c != "null"):
for a in range(len(c)):
name = c[a][len(os.path.dirname(c[a]))+1:]
info = name.split(".")[0].split(" - ")
res = down(info[0], info[1])
if(res == "null"):#没有信息
continue
r = requests.get(res)
with open(os.path.dirname(name)+"\\"+name.split(".")[0]+".lrc", "wb") as code:
code.write(r.content)
print("成功")

使用python做一个IRC在线下载器的更多相关文章

  1. 用 python 实现一个多线程网页下载器

    今天上来分享一下昨天实现的一个多线程网页下载器. 这是一个有着真实需求的实现,我的用途是拿它来通过 HTTP 方式向服务器提交游戏数据.把它放上来也是想大家帮忙挑刺,找找 bug,让它工作得更好. k ...

  2. python做一个简易图片下载工具

    代码有点乱,先这样 # -*- coding:utf-8 -*- #__author__ :kusy #__content__:文件说明 #__date__:2018/11/01 11:01 impo ...

  3. 【图文详解】python爬虫实战——5分钟做个图片自动下载器

    python爬虫实战——图片自动下载器 之前介绍了那么多基本知识[Python爬虫]入门知识,(没看的先去看!!)大家也估计手痒了.想要实际做个小东西来看看,毕竟: talk is cheap sho ...

  4. python爬虫实战——5分钟做个图片自动下载器

      python爬虫实战——图片自动下载器 制作爬虫的基本步骤 顺便通过这个小例子,可以掌握一些有关制作爬虫的基本的步骤. 一般来说,制作一个爬虫需要分以下几个步骤: 分析需求(对,需求分析非常重要, ...

  5. Python实战:美女图片下载器,海量图片任你下载

    Python应用现在如火如荼,应用范围很广.因其效率高开发迅速的优势,快速进入编程语言排行榜前几名.本系列文章致力于可以全面系统的介绍Python语言开发知识和相关知识总结.希望大家能够快速入门并学习 ...

  6. 用Python做一个知乎沙雕问题总结

    用Python做一个知乎沙雕问题总结 松鼠爱吃饼干2020-04-01 13:40 前言 本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以 ...

  7. python使用tcp实现一个简单的下载器

    上一篇中介绍了tcp的流程,本篇通过写一个简单的文件下载器程序来巩固之前学的知识. 文件下载器的流程如下: 客户端: 输入目标服务器的ip和port 输入要下载文件的名称 从服务器下载文件保存到本地 ...

  8. 用python实现的百度音乐下载器-python-pyqt-改进版

    之前写过一个用python实现的百度新歌榜.热歌榜下载器的博文,实现了百度新歌.热门歌曲的爬取与下载.但那个采用的是单线程,网络状况一般的情况下,扫描前100首歌的时间大概得到40来秒.而且用Pyqt ...

  9. 用python做一个搜索引擎(Pylucene)

    什么是搜索引擎? 搜索引擎是“对网络信息资源进行搜集整理并提供信息查询服务的系统,包括信息搜集.信息整理和用户查询三部分”.如图1是搜索引擎的一般结构,信息搜集模块从网络采集信息到网络信息库之中(一般 ...

随机推荐

  1. NodeService Ensure that Node.js is installed and can be found in one of the PATH directories

    今天发布NodeService到服务器,服务器环境是window server 2012 一直报错: [1] Ensure that Node.js is installed and can be f ...

  2. 【Alpha冲刺阶段】Day 7

    [Alpha冲刺阶段]Scrum Meeting Daily7 1.会议简述 会议开展时间 2020/5/28   8:00-8:30 PM 会议基本内容摘要 讨论合并测试问题 参与讨论人员 项目全体 ...

  3. jsonp使用post方法

    来源https://www.jb51.net/article/68980.htm

  4. centos7网卡bond配置--自己另一篇文章的补充

    这篇文章是自己另一篇文章的第二种方法的一个完善的补充 https://www.cnblogs.com/zzf0305/p/9588585.html 1 备份网卡配置文件2 使用nmcli命令配置bon ...

  5. JavaScript:防抖与节流

    ①防抖: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  6. 六、TestNG传递参数1

    TestNG可以通过testng.xml和Data Providers向测试方法传递参数 利用testNG.xml传递参数 1-创建一个TestNG测试类 其中 parameters = {" ...

  7. rhel6.4搭建rac前共享存储配置(iscsi+multipath+udev)

    rhel6.4搭建rac前共享存储配置(iscsi+multipath+udev) server: IP配置: 192.168.12.30 192.168.12.40   添加一个100G磁盘/dev ...

  8. Spark SQL 小文件问题处理

    在生产中,无论是通过SQL语句或者Scala/Java等代码的方式使用Spark SQL处理数据,在Spark SQL写数据时,往往会遇到生成的小文件过多的问题,而管理这些大量的小文件,是一件非常头疼 ...

  9. Salesforce LWC学习(二十九) getRecordNotifyChange(LDS拓展增强篇)

    本篇参考: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/data_ui_api https ...

  10. GDB —— 优化STL容器变量的显示

    步骤 wget http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt cp dbinit_stl_views-1.03.txt ...