Requests+Xpath(爬取音频)

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值