欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實(shí)現(xiàn)的下載8000首兒歌的代碼分享

 更新時(shí)間:2014年11月21日 08:58:30   投稿:junjie  
這篇文章主要介紹了Python實(shí)現(xiàn)的下載8000首兒歌的代碼分享,本文直接給出實(shí)現(xiàn)代碼,下載的是有伴網(wǎng)的資源,需要的朋友可以參考下

下載8000首兒歌的python的代碼:

復(fù)制代碼 代碼如下:

#-*- coding: UTF-8 -*-

from pyquery import PyQuery as py
from lxml import etree
import urllib
import re
import os
import sys
import logging

def format(filename):
    tuple=(' ',''','\'')
    for char in tuple:
        if (filename.find(char)!=-1):
            filename=filename.replace(char,"_")
    return filename

def download_mp3(mp3_url, filename,dir):      
    f = dir+"\\"+filename
    if os.path.exists(f):
      logger.debug(f+" is existed.")
      return
     
    try:
        open(f, 'wb').write(urllib.urlopen(mp3_url).read())
        logger.debug(  filename + ' is downloaded.')
    except:
        logger.debug( filename + ' is not downloaded.')

       
def download_all_mp3(start,end,dir,logger):
  for x in range(start,end):
    try:
        url = "http://www.youban.com/mp3-d" + str(x) + ".html"
        logger.debug(str(x) + ": "+url)
        doc = py(url=url)
        e = doc('.mp3downloadbox')
        if e is None or e == '':
          logger.debug(url+" is not existed.")
          return
         
        e = unicode(e)
        #logger.debug( e)
        regex = re.compile(ur".*<h1>(.*)</h1>.*downloadboxlist.*?<a.*?\"(.*?)\"",re.UNICODE|re.S)
        m = regex.search(e)
        if m is not None:
          title = m.group(1).strip()
          title2 = str(x)+"_"+title + ".mp3"
          #title2 = re.sub(' ','_',title2)
          title2 = format(title2)
          link = m.group(2)
          #logger.debug( "title:" + title + " link:" + link)
          if link == '' or title == '':
            logger.debug(url + " is not useful")
            continue
          logger.debug(str(x)+": "+link)
          download_mp3(link,title2,dir)
    except:
        logger.debug(url+" met exception.")
        continue
     

     
if __name__ == "__main__":
    dir_root = "e:\\song"
    if sys.argv[3] != '': dir_root=sys.argv[3]
   
    start,end = 1,8000
    if sys.argv[1] >= 0 and sys.argv[2]>=0:
      start,end = int(sys.argv[1]),int(sys.argv[2])
      print ("Download from %s to %s.\n" % (start,end))     
   
    dir = dir_root + "\\"+str(start)+"-"+str(end)
    if not os.path.exists(dir):
      os.mkdir(dir)    
    print "Download to " + dir + ".\n"
   
    logger = logging.getLogger("simple")
    logger.setLevel(logging.DEBUG) 
    fh = logging.FileHandler(dir+"\\"+"download.log")
    ch = logging.StreamHandler()
    formatter = logging.Formatter("%(message)s")
    ch.setFormatter(formatter)
    fh.setFormatter(formatter)
    logger.addHandler(ch)
    logger.addHandler(fh)
    download_all_mp3(start,end,dir,logger)

有需要的可以參考繼續(xù)修改。

相關(guān)文章

  • scrapy爬蟲遇到j(luò)s動(dòng)態(tài)渲染問(wèn)題

    scrapy爬蟲遇到j(luò)s動(dòng)態(tài)渲染問(wèn)題

    本文主要介紹了scrapy爬蟲遇到j(luò)s動(dòng)態(tài)渲染問(wèn)題,通過(guò)js渲染出來(lái)的動(dòng)態(tài)網(wǎng)頁(yè)的內(nèi)容與網(wǎng)頁(yè)文件內(nèi)容是不一樣的,本文就來(lái)解決一下這些問(wèn)題,感興趣的可以了解一下
    2022-05-05
  • Python中Django發(fā)送帶圖片和附件的郵件

    Python中Django發(fā)送帶圖片和附件的郵件

    本篇文章主要介紹了Python中Django發(fā)送帶圖片和附件的郵件 ,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。
    2017-03-03
  • Python實(shí)現(xiàn)并行抓取整站40萬(wàn)條房?jī)r(jià)數(shù)據(jù)(可更換抓取城市)

    Python實(shí)現(xiàn)并行抓取整站40萬(wàn)條房?jī)r(jià)數(shù)據(jù)(可更換抓取城市)

    本文主要是以房?jī)r(jià)網(wǎng)房?jī)r(jià)信息爬蟲為例,對(duì)Python實(shí)現(xiàn)整站40萬(wàn)條房?jī)r(jià)數(shù)據(jù)并行抓?。筛鼡Q抓取城市)的方法進(jìn)行分析介紹。需要的朋友一起來(lái)看下吧
    2016-12-12
  • python 實(shí)現(xiàn)提取PPT中所有的文字

    python 實(shí)現(xiàn)提取PPT中所有的文字

    這篇文章主要介紹了python 實(shí)現(xiàn)提取PPT中所有的文字,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-03-03
  • python深入講解魔術(shù)方法

    python深入講解魔術(shù)方法

    所謂魔法函數(shù)(Magic Methods),是Python的?種?級(jí)語(yǔ)法,允許你在類中?定義函數(shù)(函數(shù)名格式?般為_(kāi)_xx__),并綁定到類的特殊?法中。?如在類A中?定義__str__()函數(shù),則在調(diào)?str(A())時(shí),會(huì)?動(dòng)調(diào)?__str__()函數(shù),并返回相應(yīng)的結(jié)果
    2022-06-06
  • Python 常用的安裝Module方式匯總

    Python 常用的安裝Module方式匯總

    這篇文章主要介紹了Python 常用的安裝Module方式匯總,需要的朋友可以參考下
    2017-05-05
  • python中urllib.unquote亂碼的原因與解決方法

    python中urllib.unquote亂碼的原因與解決方法

    這篇文章主要給大家介紹了python中urllib.unquote亂碼的原因與解決方法,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友可以參考學(xué)習(xí),下面跟著小編一起來(lái)學(xué)習(xí)學(xué)習(xí)吧。
    2017-04-04
  • 詳解Python如何使用Self類型實(shí)現(xiàn)返回類的實(shí)例對(duì)象

    詳解Python如何使用Self類型實(shí)現(xiàn)返回類的實(shí)例對(duì)象

    在 Python 中,類方法通常會(huì)返回類的實(shí)例對(duì)象,本文將詳細(xì)介紹如何在 Python 中使用 Self 類型來(lái)返回類的實(shí)例對(duì)象,并提供豐富的示例代碼幫助更好地理解,快跟隨小編一起學(xué)習(xí)起來(lái)吧
    2024-02-02
  • 利用scikitlearn畫ROC曲線實(shí)例

    利用scikitlearn畫ROC曲線實(shí)例

    這篇文章主要介紹了利用scikitlearn畫ROC曲線實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-07-07
  • Python實(shí)現(xiàn)Linux中的du命令

    Python實(shí)現(xiàn)Linux中的du命令

    這篇文章主要介紹了Python實(shí)現(xiàn)Linux中簡(jiǎn)單du命令,需要的朋友可以參考下
    2017-06-06

最新評(píng)論