我正在编写一个基于 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
请您参考如下方法:
首先,您绝对应该开始使用 last.fm API。
并且,为了避免“太多请求”问题,如果您不使用 API,be a good web-scraping citizen并在您的请求之间引入延迟 - 您可以从像 time.sleep(delay_in_seconds) 这样简单的东西开始(当然您需要 import time)。
