python mp3,python爬取酷狗音樂_Python實例---爬去酷狗音樂

 2023-10-08 阅读 30 评论 0

摘要:項目一:獲取酷狗TOP 100 排名 文件&&歌手 時長 python mp3。效果: 附源碼: import time import json from bs4 import BeautifulSoup import requests 酷狗音樂爬蟲,class Kugou(object): def __init__(self): self.header = { "User-A

項目一:獲取酷狗TOP 100

排名

519608-20180909165202061-2130518844.png

文件&&歌手

519608-20180909165204264-1174783563.png

時長

519608-20180909165206258-1382435735.png

python mp3。效果:

519608-20180909165207964-1296380096.png

附源碼: import time

import json

from bs4 import BeautifulSoup

import requests

酷狗音樂爬蟲,class Kugou(object):

def __init__(self):

self.header = {

"User-Agent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'

}

python爬取音樂。def getInfo(self, url):

html = requests.get(url, headers=self.header)

soup = BeautifulSoup(html.text, 'html.parser')

# print(soup.prettify())

ranks = soup.select('.pc_temp_num')

利用python爬取。titles = soup.select('.pc_temp_songlist > ul > li > a') # 層層標簽查找

times = soup.select('.pc_temp_time')

for rank, title, songTime in zip(ranks, titles, times):

data = {

# rank 全打印就是帶HTML標簽的

用python寫網絡爬蟲、'rank': rank.get_text().strip(),

'title': title.get_text().split('-')[1].strip(),

'singer': title.get_text().split('-')[0].strip(),

'songTime': songTime.get_text().strip()

}

python爬蟲教程,s = str(data)

print('rank:%2s\t' % data['rank'], 'title:%2s\t' % data['title'], 'singer:%2s\t' %data['singer'], 'songTime:%2s\t' % data['songTime'])

with open('hhh.txt', 'a', encoding='utf8') as f:

f.writelines(s + '\n')

if __name__ == '__main__':

python gui、urls = [

'http://www.kugou.com/yy/rank/home/{}-8888.html'.format(str(i)) for i in range(30)

]

kugou = Kugou()

for url in urls:

python編程。kugou.getInfo(url)

time.sleep(1)

部分代碼解析--------------------------------------------------------------------

urls = ['http://www.kugou.com/yy/rank/home/{}-8888.html'.format(str(i)) for i in range(1, 5)]

for i in urls:

python3,print(i)

結果打印:

http://www.kugou.com/yy/rank/home/1-8888.html

http://www.kugou.com/yy/rank/home/2-8888.html

http://www.kugou.com/yy/rank/home/3-8888.html

python為什么叫爬蟲?http://www.kugou.com/yy/rank/home/4-8888.html

--------------------------------------------------------------------

for rank, title, songTime in zip(ranks, titles, times):

data = {

# rank 全打印就是帶HTML標簽的

python爬取qq音樂歌單、'rank': rank.get_text().strip(),

'title': title.get_text().split('-')[0].strip(),

'singer': title.get_text().split('-')[1].strip(),

'songTime': songTime.get_text()

}

用python爬取網站數據。print(data['rank'])

print(data['title'])

print(data['singer'])

print(data['songTime'])

結果打印:

python爬蟲下載歌曲?1

飛馳于你

許嵩

4: 04

--------------------------------------------------------------------

for rank, title, songTime in zip(ranks, titles, times):

data = {

# rank 全打印就是帶HTML標簽的

'rank': rank,

'title': title,

'songTime': songTime

}

print(data['rank'])

print(data['title'])

print(data['songTime'])

結果打印:

1

許嵩 - 飛馳于你

4:04

項目二:搜索曲目獲取URL

根據關鍵字搜索后的結果:

519608-20180909173212661-518120689.png# encoding=utf-8

# Time : 2018/4/27

# Email : z2615@163.com

# Software: PyCharm

# Language: Python 3

import requests

import json

class KgDownLoader(object):

def __init__(self):

self.search_url = 'http://songsearch.kugou.com/song_search_v2?callback=jQuery191034642999175022426_1489023388639&keyword={}&page=1&pagesize=30&userid=-1&clientver=&platform=WebFilter&tag=em&filter=2&iscorrection=1&privilege_filter=0&_=1489023388641'

# .format('園游會')

self.play_url = 'http://www.kugou.com/yy/index.php?r=play/getdata&hash={}'

self.song_info = {

'歌名': None,

'演唱者': None,

'專輯': None,

'filehash': None,

'mp3url': None

}

def get_search_data(self, keys):

search_file = requests.get(self.search_url.format(keys))

search_html = search_file.content.decode().replace(')', '').replace(

'jQuery191034642999175022426_1489023388639(', '')

views = json.loads(search_html)

for view in views['data']['lists']:

song_name = view['SongName'].replace('', '').replace('', '')

album_name = view['AlbumName'].replace('', '').replace('', '')

sing_name = view['SingerName'].replace('', '').replace('', '')

file_hash = view['FileHash']

new_info = {

'歌名': song_name,

'演唱者': sing_name,

'專輯': album_name if album_name else None,

'filehash': file_hash,

'mp3url': None

}

self.song_info.update(new_info)

yield self.song_info

def get_mp3_url(self, filehash):

mp3_file = requests.get(self.play_url.format(filehash)).content.decode()

mp3_json = json.loads(mp3_file)

real_url = mp3_json['data']['play_url']

self.song_info['mp3url'] = real_url

yield self.song_info

def save_mp3(self, song_name, real_url):

with open(song_name + ".mp3", "wb")as fp:

fp.write(requests.get(real_url).content)

if __name__ == '__main__':

kg = KgDownLoader()

mp3_info = kg.get_search_data(input('請輸入歌名:'))

for x in mp3_info:

mp3info = kg.get_mp3_url(x['filehash'])

for i in mp3info:

print(i)

519608-20180909172608131-1573324889.png

項目三:搜索下載歌曲

代碼僅供學習參考from selenium import webdriver

from bs4 import BeautifulSoup

import urllib.request

from selenium.webdriver.common.action_chains import ActionChains

input_string = input('>>>please input the search key:')

#input_string="你就不要想起我"

driver = webdriver.Chrome()

driver.get('http://www.kugou.com/')

a=driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[1]/input') #輸入搜索內容/html/body/div[1]/div[1]/div[1]/div[1]/input

a.send_keys(input_string)

driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[1]/div[1]/div/i').click() #點擊搜索/html/body/div[1]/div[1]/div[1]/div[1]/div/i

for handle in driver.window_handles:#方法二,始終獲得當前最后的窗口,所以多要多次使用

driver.switch_to_window(handle)

#result_url = driver.current_url

#driver = webdriver.Firefox()

#driver.maximize_window()

#driver.get(result_url)

#j=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[2]/div[1]/a').get_attribute('title')測試

#print(j)

soup = BeautifulSoup(driver.page_source,'lxml')

PageAll = len(soup.select('ul.list_content.clearfix > li'))

print(PageAll)

for i in range(1,PageAll+1):

j=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[%d]/div[1]/a'%i).get_attribute('title')

print('%d.'%i + j)

choice=input("請輸入你要下載的歌曲(輸入序號):")

#global mname

#mname=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[%d]/div[1]/a'%choice).get_attribute('title')#歌曲名

a=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[%s]/div[1]/a'%choice)#定位

b=driver.find_element_by_xpath('/html/body/div[4]/div[1]/div[2]/ul[2]/li[%s]/div[1]/a'%choice).get_attribute('title')

actions=ActionChains(driver)#selenium中定義的一個類

actions.move_to_element(a)#將鼠標移動到指定位置

actions.click(a)#點擊

actions.perform()

#wait(driver)?

#driver = webdriver.Firefox()

#driver.maximize_window()

#driver.get(result_url)

#windows = driver.window_handles

#driver.switch_to.window(windows[-1])

#handles = driver.window_handles

for handle in driver.window_handles:#方法二,始終獲得當前最后的窗口,所以多要多次使用

driver.switch_to_window(handle)

Local=driver.find_element_by_xpath('//*[@id="myAudio"]').get_attribute('src')

print(driver.find_element_by_xpath('//*[@id="myAudio"]').get_attribute('src'))

def cbk(a, b, c):

per = 100.0 * a * b / c

if per > 100:

per = 100

print('%.2f%%' % per)

soup=BeautifulSoup(b)

name=soup.get_text()

path='D:\%s.mp3'%name

urllib.request.urlretrieve(Local, path, cbk)

print('finish downloading %s.mp3' % name + '\n\n')

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://808629.com/136743.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 86后生记录生活 Inc. 保留所有权利。

底部版权信息