欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Requests+Xpath(爬取音频)

程序员文章站 2022-03-02 20:52:38
...
import requests
from lxml import etree
import json

headers = {
        'User-Agent': r'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko)'
                        r'Chrome/47.0.2526.108 Safari/537.36 2345Explorer/8.8.3.16721',
        'Referer': r'http://www.ximalaya.com/',
        'Connection': 'keep-alive'
    }

def getmusic(url):
    r=requests.get(url,headers=headers).text
    a=json.loads(r)
    name=a['title']
    music=a['play_path_64']
    data=requests.get(music).content
    with open('%s.m4a'%name,'wb') as f:
        f.write(data)

def getUrl(Url):
    r=requests.get(Url,headers=headers).text
    IDs=etree.HTML(r).xpath('//*[@id="mainbox"]/div[1]/div[1]/div/div[2]/@sound_ids')[0].split(',')
    for ID in IDs:
        b='http://www.ximalaya.com/tracks/'+ID+'.json'
        getmusic(b)

for i in range(1,4):
    getUrl('http://www.ximalaya.com/66261471/album/7183288?page=%d'%i)

更多爬虫实例请见 https://blog.csdn.net/weixin_39777626/article/details/81564819