python爬取喜马拉雅FM雪中悍刀行整本有声小说!下次教你们爬付费!

302 阅读1分钟

前言

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

开发工具

  • python 3.6.5

  • pycharm

    import requests import re

相关模块可pip安装

确定网页目标

有个白狐儿脸,佩双刀绣冬春雷,要做那天下第一。湖底有白发老魁爱吃荤。缺门牙老仆背剑匣。山上有个骑青牛的年轻师叔祖,不敢下山。有个骑熊猫扛向日葵不太冷的少女杀手。 这个江湖,高人出行要注重出尘装扮,女侠行走江湖要注意培养人气,宗派要跟庙堂打好关系。 而主角,则潇洒带刀,把江湖捅了一个通透…

![](https://p1-tt-ipv6.byteimg.com/large/pgc-image/bd11c9da55304ec0afb1415dd818c018)

模拟浏览器请求网页

  • 获取网页数据

    import requests headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36' } url = 'www.ximalaya.com/youshengshu…' response = requests.get(url=url, headers=headers)

解析网页数据

保存音频文件

def download(url, title):
    filename = 'D:\\python\\demo\\喜马拉雅\\FM\\' + title + '.mp3'
    response = requests.get(url=url, headers=headers)
    with open(filename, mode='wb') as f:
        f.write(response.content)
![](https://p26-tt.byteimg.com/large/pgc-image/9f1e7f5b90224418b669988bf886660c)
![](https://p1-tt-ipv6.byteimg.com/large/pgc-image/584ea9b77afd4e9ba0b3d5d30c36714f)

你学会了吗?完整项目代码获取后台私信小编01即可!