声控党的福利!Python爬取【猫耳FM】音频数据,用多线程对比通用爬虫的速度
前言
今天的“受害者”为【猫耳FM】,一个音频网站
地址:https://www.missevan.com/sound/m/110
- requests
- time
- re
- concurrent.futures
- 版 本:anaconda5.2.0(python3.6.5)
- 编辑器:pycharm
import time import requests import concurrent.futures import re
def get_html(url): response = requests.get(url) return response
def parse(response): mp3_ids = re.findall('<a target="_player" href="/sound/(.*?)" title=".*?">', response.text) return mp3_ids
def parse_2(response): json_data = response.json() title = json_data['info']['sound']['soundstr'] soundurl = json_data['info']['sound']['soundurl'] return title, soundurl
def save(title, mp3_data): with open('mp3\\' + title + '.mp3', mode='wb') as f: f.write(mp3_data) print(title, '下载完成!!!')
def change_title(title): new_title = re.sub(r'[\//|:?<>"*]', '_', title) return new_title
# 1. 发送请求 response = get_html(url) # 2. 解析数据 soundid mp3_ids = parse(response) for mp3_id in mp3_ids: # 3. 请求另外详情页 地址拼接 https://www.missevan.com/sound/getsound?soundid=3922170 mp3_url = 'https://www.missevan.com/sound/getsound?soundid=' + mp3_id resp_2 = get_html(mp3_url) # 4. 解析音频url地址 音频标题 title, soundurl = parse_2(resp_2) # 修改标题 title = change_title(title) # 5. 请求音频url地址 音频 二进制数据 content mp3_data = get_html(soundurl).content # 6. 下载保存 到本地 save(title, mp3_data)
start_time = time.time() for page in range(1, 5): print(f'----------正在爬取第{page}页-------------') run(f'https://www.missevan.com/sound/m?id=110&p={page}') print('一共花费了:', time.time()-start_time)
if __name__ == '__main__': start_time = time.time() with concurrent.futures.ThreadPoolExecutor(max_workers=1000) as executor: for page in range(1, 5): url = f'https://www.missevan.com/sound/m?id=110&p={page}' executor.submit(run, url) print('一共花费了:', time.time()-start_time)