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. RocketMQ开发者指南

    1. 概念和特性 概念:介绍RocketMQ的基本概念模型 1 消息模型(Message Model) RocketMQ主要由 Producer.Broker.Consumer 三部分组成,其中Pro ...

  2. webpack项目如何正确打包引入的自定义字体

    webpack项目如何正确打包引入的自定义字体 一. 如何在Vue或React项目中使用自定义字体 在开发前端项目时,经常会遇到UI同事希望在项目中使用一个炫酷字体的需求.那么怎么在项目中使用自定义字 ...

  3. ajax的五种状态

    ajax的五种状态(readyState ) 0 - (未初始化)还没有调用send()方法 1 - (载入)已调用send()方法,正在发送请求 2 - (载入完成)send()方法执行完成,已经接 ...

  4. AtCoder Regular Contest 108

    Contest Link Official Editorial A - Sum and Product Given are integers \(S\) and \(P\) . Is there a ...

  5. 【补题记录】ZJU-ICPC Summer Training 2020 部分补题记录

    补题地址:https://zjusummer.contest.codeforces.com/ Contents ZJU-ICPC Summer 2020 Contest 1 by Group A Pr ...

  6. antDesign获取表单组件的值

    子组件中:  getFormValue是在点击确定按钮获取表单值得事件函数,一旦执行就会执行里边的validate()回调函数 返回的数据中有error和value两种,如果存在error那就是其中某 ...

  7. pytest接口测试轻松入门

    通过Postman请求结果如下图: 那我们怎么用pytest进行测试呢? 在接口测试,我们要用到requests包,实现代码如下: import pytest import allure import ...

  8. iOS label 添加删除线(删划线)遇到的坑

    1.添加删划线方法遇到的问题 -(void)lastLabelDeal:(NSString *)str1 string:(NSString *)str2 label:(UILabel *)label{ ...

  9. (一)NumPy基础:数组和矢量计算

    一.创建ndarray 1.各种创建函数的使用 import numpy as np #创建ndarray #1.array方法 data1 = [[6, 7.5, 8, 0, 1], [2, 8, ...

  10. 基于注解的实现获取微信openId1

    最近在弄微信支付,网站有好几种不同类型的"商品",去每个支付的页面都需要获取用户的OpenId,而且获取openid要在微信的浏览器去发送请求,如果有三个不同类型的付款页面就需要写 ...