1. 程式人生 > >爬取qingting.fm的頻道音頻~~

爬取qingting.fm的頻道音頻~~

wapi () sleep firefox total 如果 int write 使用

通過學習xmly的爬取

自己琢磨出的qingtingfm爬取頻道視頻

特記錄一下

 1 # -*- coding: utf-8 -*-
 2 import requests, time
 3 import os
 4 import sys
 5 reload(sys)
 6 sys.setdefaultencoding(utf-8)
 7 
 8 hd = {User-Agent: Mozilla/5.0 (Windows NT 10.0; rv:55.0) Gecko/20100101 Firefox/55.0}
 9 url_top = http://i.qingting.fm/wapi/channels/
# 蜻蜓各個json信息的接口 10 m4aurl_top = http://od.qingting.fm/ 11 12 13 def getChannelInfo(channelid): 14 res = requests.get(url_top + str(channelid), headers=hd).json() 15 if int(res[code]) == 0: # code碼0正常 1 無數據 16 # (id數字不對返回是code為1 ),如果id是字符串,直接返回404,後面會報異常 17 return res 18 else
: 19 # print ‘頻道ID不存在!‘ 20 return 21 22 def getChannelName(channelid): 23 res = requests.get(url_top + str(channelid), headers=hd).json() 24 if int(res[code]) == 0: # code碼0正常 1 無數據 25 # (id數字不對返回是code為1 ),如果id是字符串,直接返回404,後面會報異常 26 return res[data][name]
27 else: 28 return 29 30 def getM4aList(channelid, page): 31 url = url_top + %s/programs/page/%d % (str(channelid), page) # 拼接頻道內音頻的信息json,page是頁碼 32 res = requests.get(url, headers=hd).json() 33 return res 34 35 36 def mkdir(name): 37 path = E:\\蜻蜓fm下載\\ + name 38 isExists = os.path.exists(path) 39 if not isExists: 40 os.makedirs(path) 41 print 創建***%s***文件夾成功!開始下載 % name 42 return True 43 else: 44 print 已存在***%s***文件夾!開始下載 % name 45 return False 46 47 48 def download(url, name): 49 m4a = requests.get(url, headers=hd).content 50 with open(name + .m4a, wb) as f: 51 f.write(m4a) 52 53 while True: 54 try: 55 channelid=raw_input(unicode(請輸入要下載的頻道ID(純數字): ).encode(gbk)) 56 # raw_input在cmd裏中文亂碼,使用強制轉碼解決,先轉碼 unicode(str) 再編碼 .encode(‘gbk‘) 57 channelid =int(channelid) 58 break 59 except: 60 print uID是數字不是字母!OK?! 61 pass 62 63 chname = getChannelName(channelid) 64 if chname==None: 65 print 頻道ID不存在! 66 else: 67 res=getChannelInfo(channelid) 68 chtype=res[data][type] 69 chsale=res[data][sale_type] 70 print chsale==5 71 if chtype==channel_live: 72 print u在線節目,不能下載! 73 elif chsale==5: 74 print u付費頻道,不能下載! 75 else: 76 mkdir(chname) 77 os.chdir(E:\\蜻蜓fm下載\\ + chname) 78 i = 1 79 j = 1 80 while True: 81 m4alist = getM4aList(channelid, i) # page參數從1開始,每次while遞增1 82 code = m4alist[code] 83 i += 1 84 if int(code) == 1: # code碼控制while循環,正常返回0,頁碼超出範圍返回1 85 break # 返回1,退出while循環 86 else: 87 m4ainfos = m4alist[data] 88 total = m4alist[total] 89 for m4ainfo in m4ainfos: 90 m4aname = m4ainfo[name] 91 m4aurl = m4aurl_top + m4ainfo[file_path] 92 print u*********下載第%s個 共%s個********* % (str(j), total) 93 download(m4aurl, m4aname) 94 print m4aname + u 下載成功!~ 95 time.sleep(1) 96 j += 1

爬取qingting.fm的頻道音頻~~