gevent 實現網易雲音樂歌曲下載

下載是半手動的(需要自己輸音樂的ID並且提供命名),因爲辦了QQ綠鑽會員,不想再辦網易的,純粹自己用着方便

比較侷限的是隻能下載本身可以聽的歌,會員才能聽的文件下載不了。

(網易現在歌曲即使可以聽,下載也是需要會員的,這個方法可以避開因此而充值會員)

 

音樂ID,複製右鍵歌曲,複製網頁鏈接即可獲得:)

比如這個:http://music.163.com/#/m/song?id=29431062

就是一串數字,然後運行文件(在終端或者任何可以運行.py文件的環境都可以),把數字輸入,再輸入文件名即可

注意:request模塊要在gevent的補丁模塊之前導入

import gevent
import urllib.request
from gevent import monkey
monkey.patch_all()


def download_song(img_url, img_name):
    try:
        img = urllib.request.urlopen(img_url)
        with open(img_name, 'wb') as file:
            while True:
                img_data = img.read(1024)
                if img_data:
                    file.write(img_data)
                else:
                    break
    except Exception as e:
        print('下載失敗', e)
    else:
        print('下載成功', img_name)


if __name__ == '__main__':
    id = input('請輸入音樂ID: ')
    song = 'http://music.163.com/song/media/outer/url?id='+ id +'.mp3'
    name = input('請輸入文件名字:')
    # 沒搞服務器,不能查歌后自動抓ID匹配,小工具,湊合用
    g1 = gevent.spawn(download_song, song, name+'.mp3')

    g1.join()

 

 

PS:只能爬大衆資源,網易做了版權保護的格式搞不來

 

 

 

注意:request模塊要在gevent的補丁模塊之前導入

報錯信息:

/Users/cici/PycharmProjects/Web_crawlers/douban_spider_multi_.py:447: MonkeyPatchWarning: Monkey-patching ssl after ssl has already been imported may lead to errors, including RecursionError on Python 3.6. It may also silently lead to incorrect behaviour on Python 3.7. Please monkey-patch earlier. See https://github.com/gevent/gevent/issues/1016. Modules that had direct imports (NOT patched): ['urllib3.util.ssl_ (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/util/ssl_.py)', 'urllib3.util (/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/urllib3/util/__init__.py)']. 

  monkey.patch_all()

 

解決原理:

https://github.com/gevent/gevent/issues/1016

Thanks. The problem is that you're importing requests before you monkey patch. You must monkey patch before importing anything else. I can reproduce this if I patch in the incorrect order, but if I patch in the correct order it works as expected:

 

解決方法:

代碼上就是把requests模塊放到monkey.patch_all前導入。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章