前言
本文的文字及圖片來源于網路,僅供學習、交流使用,不具有任何商業用途,著作權歸原作者所有,如有問題請及時聯系我們以作處理,
本次目標
爬取喜馬拉雅音頻
https://www.ximalaya.com/
開發工具
- python 3.6.5
- pycharm

爬蟲代碼
匯入工具
import requests import re import time
請求網頁
headers = { 'cookie': 'device_id=xm_1596531699133_kdfpr35pt5o0on; _xmLog=h5&b145d793-85e1-4aec-8cf3-25643943c990&2.1.2; x_xmly_traffic=utm_source%253A%2526utm_medium%253A%2526utm_campaign%253A%2526utm_content%253A%2526utm_term%253A%2526utm_from%253A; Hm_lvt_4a7d8ec50cfd6af753c4f8aee3425070=1600235340,1600499992,1602060323,1602060364; Hm_lpvt_4a7d8ec50cfd6af753c4f8aee3425070=1602060571', '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 = 'https://www.ximalaya.com/youshengshu/2684034/p{}/'.format(page) response = requests.get(url=url, headers=headers)
決議網頁資料
lis = re.findall('<a title="(.*?)" href="https://www.cnblogs.com/hhh188764/p/(.*?)">', response.text, re.S)[4:-1] for i in lis: title = i[0] num_id = i[1].split('/')[-1] mp3_url = 'https://www.ximalaya.com/revision/play/v1/audio?id={}&ptype=1'.format(num_id) response_2 = requests.get(url=mp3_url, headers=headers) data = response_2.json()
保存資料
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) print('{}下載完成'.format(title))
運行代碼,效果如下圖
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/166776.html
標籤:Python
上一篇:爬蟲的基本概念
下一篇:spider.2-爬蟲的基礎
