hugang 发表于 2015-4-22 02:07:29

python写的批量下载baidu mp3的程序 至少到09-9-18仍然可用

  windows平台下的Py2.6 移植到linux应该也很容易
默认10线程下载

  其中多线程下载部分 是参考 http://hi.baidu.com/zjw0358/blog

mydown.py
#!/usr/bin/env python
# coding=utf-8
import httplib,urllib,urllib2
import re,os
from downmp3 import GetSize,DownMp3
def BaiduUrlDecode(enurl):
    import string
    from urllib import unquote
    k = u'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
    decoded = ''
    # print enurl
    try:
      key = k.index(u't') - k.index(enurl)
      for i in range(enurl.__len__()):
            char = enurl
            if char in k:
                decoded += k[(k.index(char) + key) % 62]
            else:
                decoded += char
    except IndexError:
      print 'enurl IndexError:',enurl,'$'
      # print 'enurl',enurl
    return unquote(decoded.encode('gbk'))
def BadiuUrlProcess(baidu_url):
    import re
    from urllib import quote
    match_CHchar = r'([^:._,~`!@#\|{}\^\*\(\);%/\"\'\[\]\+\=\?\-\$\&\\\w]+)' #匹配中文
    ch_pattern = re.compile(match_CHchar)
    page_url = baidu_url
    if ch_pattern.search(baidu_url):
      page_url = ch_pattern.sub(quote(ch_pattern.search(baidu_url).groups().encode('gbk')),baidu_url)
    return page_url
   

if __name__ == "__main__":
   
    print """   支持百度MP3的大部分列表音乐的下载,默认采用10线程下载
   
       新歌top100
       歌曲top500
       歌手top200 (暂不支持下载)
       中文金曲榜
       经典老歌
       热舞dj
       流金岁月
       电视金曲
       歌曲列表
       退出
                              --by auxten auxtenwpcgmailcom
    """.decode('utf-8').encode('gbk')
    id = int(raw_input('输入你想下载的list的编号: '.decode('utf-8').encode('gbk')))
    if id == 1: topid = '/list/newhits.html?id=1?top1'
    elif id == 2: topid = '/topso/mp3topsong.html?id=1?top2'
    elif id == 3: topid = '/list/tvs.html?id=1?top5';exit(1)
    elif id == 4: topid = '/list/bangping.html?id=1'#;exit(1)
    elif id == 5: topid = '/list/oldsong.html?top6'
    elif id == 6: topid = '/list/dj.html'
    elif id == 7: topid = '/list/liujinsuiyue.html'
    elif id == 8: topid = '/list/tvs.html?id=1?top5'
    elif id == 9: topid = '/list/tvs.html?id=1?top5'#;exit(1)
    elif id == 0: exit(1)
# topid = '/list/oldsong.html?top6'
    # topid = '/list/tvs.html?id=1?top5'
    print "Processing please wait.:)"
    errorlist = []
    conn = httplib.HTTPConnection('list.mp3.baidu.com')
    conn.request("GET",topid)
    response = conn.getresponse()
    html = response.read().decode('gb18030')
    # print html.encode('gbk')
    conn.close()
    match_type1 = r'">(\d{,3})\.' #编号
    match_type2 = r'">(\d{,3})\.[\s\S]*?(.*?)[^)].*' #no url songname for 流金岁月
    match_type3 = r'">(\d{,3})\.[\s\S]*?(.*?)[^)].*">(.*)\)' #name+author
    match_type4 = r'[\s]*?(\d{,3})[\s\S]*?(.*?)[\s\S]*?target="_blank">(.*?)' #no url songname singer 中文金曲榜
    list_number = re.findall(match_type1, html)
    list_all = re.findall(match_type3, html)
    if list_all == []:
      # print 'list_all empty1!'
      list_all = re.findall(match_type2, html)
    if list_all == []:
      # print 'list_all empty2!'
      list_all = re.findall(match_type4, html)
    # print 'list_all',list_all
    # print 'list_all__len__',list_all.__len__()
    conn = httplib.HTTPConnection('mp3.baidu.com')
    songnumlst = range(0,list_all.__len__())
   
    for num in songnumlst:
      try:
            try: authorname = '-'+list_all
            except IndexError:
                authorname = ''
            print list_all.encode('gbk'),list_all.encode('gbk'),authorname.encode('gbk')      
    #      print num
            conn.request("GET",BadiuUrlProcess(list_all))
            # print "URL!",BadiuUrlProcess(list_all).encode('gbk')
            response = conn.getresponse()
            html = response.read().decode('gb18030')
            conn.close()
            # print html.encode('gbk')
            html = re.search(r'
页: [1]
查看完整版本: python写的批量下载baidu mp3的程序 至少到09-9-18仍然可用