Форум сайта python.su
0
#!/usr/bin/python # -*- coding: utf-8 -*- import urllib2 import ast import re import os def get_items_of_post(address): url = "https://api.vk.com/method/wall.getById?posts=" + address + "&extended=1©_history_depth=2" response = urllib2.urlopen(url) text = response.read() # переводим из текста в словарь post_info = ast.literal_eval(text) if post_info['response']['wall'] == []: return [] else: return post_info['response']['wall'][0]['attachments'] address = raw_input("Enter string of two right numbers of url of post\n") music = get_items_of_post(address) for attachment in music: type_of_attachment = attachment['type'] if type_of_attachment != 'audio': pass else: artist = attachment[type_of_attachment]['artist'] title = attachment[type_of_attachment]['title'] name_of_file = artist + ' - ' + title + '.mp3' url = attachment[type_of_attachment]['url'] if url == '': # предусмотрен случай, когда аудиозапись удалена из общего доступа print name_of_file + " was removed from public access" continue url = re.sub('\\\\\/', '/', url) # вместо простых слэшей изначально записываются `\\/`, поэтому нужно их заменить на просто слэши во всей строке url = re.sub('\?.*', '', url) # убираем все символы после расширения `.mp3`, они нам не нужны response = urllib2.urlopen(url) if not os.path.exists(artist): os.mkdir(artist) # вот тут проблема dirfile = os.path.join(artist, name_of_file) f = open(dirfile, 'w') f.write(response.read()) print name_of_file + " was downloaded" # оповещаем о том, что файл успешно скачался
-59508827_62883 The XX - Intro.mp3 was downloaded Traceback (most recent call last): File "C:/Users/Администратор/Desktop/vkgm.py", line 42, in <module> f = open(dirfile, 'w') IOError: [Errno 2] No such file or directory: 'The XX \\The XX - Crystalised.mp3'
Офлайн
0
убрал пробелы
nazv = artist.split() if not os.path.exists(nazv): os.mkdir(nazv) #dir = os.mkdir(artist) dirfile = os.path.join(nazv, name_of_file) f = open(dirfile, 'w')
Users/Администратор/Desktop/vkgm.py”, line 39, in <module>Отредактировано widg (Янв. 29, 2016 10:10:01)
Офлайн
0
nazv = str(nazv)
Офлайн