【问题标题】:album art grabber python专辑封面抓取器 python
【发布时间】:2016-04-25 09:57:08
【问题描述】:

我正在编写一个基于 python 的应用程序,它会遍历所有音乐文件并下载他们各自的专辑封面。 我正在使用 beautifulsoup4 从 last.fm 网站上删除专辑封面。

有没有更好的方法?因为有时我会收到对网站的太多请求的例外情况。 就像 last.fm api 一样,它给了我我需要的东西,而不是报废。

我找到了这个,但我认为它不能按我的需要使用 https://github.com/pylast/pylast

我没有尝试过使用它,但我现在是这样做的:

def getAlbumArt(songDet):
   #create a search url according the name of the given song's album
   try:
       # deletes unnecessary words from the name
        if songDet.albumName.rfind("(") != -1:
            r = requests.get("http://www.last.fm/search?q="+songDet.albumName[:songDet.albumName.rfind("(")])
        else:
            r = requests.get("http://www.last.fm/search?q=" + songDet.albumName)
        html = bs4.BeautifulSoup(r.content, "html.parser")
        imagesLinks = html.find_all("ol")
        r.close()
        for imageLink in imagesLinks:
            for image in imageLink.contents:
                if type(image) is bs4.Tag:
                    if image.text.__contains__(songDet.artist):
                        # todo add path to save pic to the mp3 path
                        urllib.urlretrieve(image.find("img").attrs['src'], image.find("img").attrs[u'alt'] + ".jpg")
                        print "got a picture"
                        return True
                        #break
            #break
   except requests.RequestException as e:
       print e

【问题讨论】:

  • 您尝试过吗?如果是这样,你能提供一个你所拥有的代码示例吗?
  • 我没有尝试过使用它,但这就是我现在的做法pastebin.ca/3583239
  • 嘿@Liran,下次确保将代码添加到您的问题中。这次我替你做了。

标签: python python-2.7 beautifulsoup


【解决方案1】:

首先,您绝对应该开始使用last.fm API。

并且,为了避免“请求过多”问题,如果您不使用 API,be a good web-scraping citizen 并在您的请求之间引入延迟 - 您可以从简单的 time.sleep(delay_in_seconds) 开始(您需要 @987654324 @当然)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-04
    • 1970-01-01
    相关资源
    最近更新 更多