前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >16.ajax_case09

16.ajax_case09

作者头像
hankleo
发布2020-09-17 11:01:56
3360
发布2020-09-17 11:01:56
举报
文章被收录于专栏:Hank’s BlogHank’s Blog
代码语言:javascript
复制
import requests
import json
import re
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from multiprocessing import Pool

def download_music(songmid, music_name):
    url = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?g_tk=872989112&jsonpCallback=MusicJsonCallback06459212607938936&loginUin=11297258&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0&cid=205361747&callback=MusicJsonCallback06459212607938936&uin=11297258&songmid={0}&filename=C100{0}.m4a&guid=9136027940'.format(
        songmid)
    html = requests.get(url)
    # 去掉jsonp
    music_json = json.loads(re.findall(r'^\w+\((.*)\)$', html.text)[0])
    filename = music_json['data']['items'][0]['filename']
    vkey = music_json['data']['items'][0]['vkey']
    download_url = 'http://dl.stream.qqmusic.qq.com/{}?vkey={}&fromtag=66'.format(
        filename, vkey)
    print(download_url)
    # 下载到本地
    music = requests.get(download_url)
    # 文件名去除特殊符号
    with open("d:\\music\\{}.m4a".format(re.sub(r'[\s+|@<>:\\"/]', '', music_name)), "wb") as m:
        m.write(music.content)

def view_html():
    # qq音乐页面是js加载的,这里用chrome headless模式访问
    option = webdriver.ChromeOptions()
    option.add_argument('headless')
    driver = webdriver.Chrome(
        r'D:\Program Files\Python\chromedriver.exe',
        chrome_options=option)

    # 排行榜页面
    driver.get('https://y.qq.com/n/yqq/toplist/26.html')
    print(driver.title)
    try:
        # 等待播放列表加载完毕
        WebDriverWait(
            driver, 10).until(
            EC.presence_of_element_located(
                (By.CLASS_NAME, "songlist__songname_txt")))

        lis = driver.find_elements_by_class_name('songlist__songname_txt')
        pattern = re.compile(r'https://y.qq.com/n/yqq/song/(\S+).html')
        for i in range(lis.__len__()):
            li = lis.__getitem__(i)
            a = li.find_element_by_class_name('js_song')
            # 获得songid
            href = a.get_attribute('href')
            music_name = a.get_attribute('title')
            m = pattern.match(href)
            download_music(m.group(1), music_name)

    finally:
        driver.quit()

if __name__ == '__main__':
    view_html()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-05-04 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档