您当前的位置:首页 > 计算机 > 编程开发 > Python

python爬虫 爬取mp3

时间:11-05来源:作者:点击数:

python爬虫 爬取mp3

import requests
from lxml import etree
import os

if __name__=='__main__':
    if not os.path.exists('xiaofang'):
        os.mkdir('xiaofang')

    url='https://www.yespik.com/search-sound/77822.html'

    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.93 Safari/537.36'
    }

    resp = requests.get(url=url, headers=headers).text
    # fp=open('a.html','w',encoding='utf-8')
    # fp.write(resp)
    # fp.close()

    #数据解析
    tree=etree.HTML(resp)
    ul_list=tree.xpath('/html/body/div[4]/div[1]/div')
    # print(ul_list)
    for li in ul_list:
        wang_url = li.xpath('div/div[3]/audio/source/@src')[0]
        wang_url='http:'+ wang_url
        print(wang_url)
        name= li.xpath('div/div[2]/a/text()')[0]+'.mp3'
        print(name)
        data1 = requests.get(url=wang_url, headers=headers)
        data2=data1.content
        # # 压缩包路径
        page3_path = 'xiaofang/' + name
        with open(page3_path, mode='wb') as fp:
            fp.write(data2)
            print(name, "下载完成!!!")
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门