爬虫之多线程,提高效率

arr = ["http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3",
"http://music.163.com/song/media/outer/url?id=1820550501.mp3"]

import random
def downsong(url):
    res = requests.get(url,headers=headers)
    with open(f"D://music/{str(random.randint(1,100))}.mp3","wb")as f:
        f.write(res.content)

from multiprocessing.dummy import Pool
p = Pool(4)
p.map(downsong,arr)
print("下载完毕")
View Code

下载网易云 使用多线程爬虫

posted @ 2021-02-19 00:15    阅读(40)  评论(0编辑  收藏  举报