Created
May 24, 2017 14:14
-
-
Save todatasudata/417b5a8583069f88af31d09040b8324f to your computer and use it in GitHub Desktop.
Script to download all photos from certain album on vk.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ''' | |
| Example: | |
| python3 get_vk_album.py https://vk.com/album405303935_244066721 | |
| Supports not more than 1000 photos in album. | |
| ''' | |
| import requests | |
| import json | |
| from bs4 import BeautifulSoup | |
| import os | |
| import sys | |
| import re | |
| url = sys.argv[1] | |
| ids = re.findall(r'[0-9-]+', url) | |
| owner_id, album_id = ids | |
| token = 'YOUR TOKEN' | |
| raw_page = requests.get(url) | |
| #create directory for photos (same name as album's one) | |
| soup = BeautifulSoup(raw_page.text, 'html.parser') | |
| dirname = soup.title.text | |
| try: | |
| os.mkdir(dirname) | |
| except FileExistsError: | |
| print('Directory already exists') | |
| request_base = 'https://api.vk.com/method/photos.get?' | |
| request = request_base + 'owner_id=' + owner_id + '&album_id=' + album_id + '&token=' + token | |
| response = requests.get(request) | |
| read_response = json.loads(response.text) | |
| for item in read_response['response']: | |
| filename = re.findall('[\w-]*\.jpg', item['src_big'])[0] | |
| filename = os.path.join(dirname, filename) | |
| print(filename) | |
| if not os.path.isfile(filename): | |
| with open(filename, 'wb') as file: | |
| photo = requests.get(item['src_big'], stream=True).content | |
| file.write(photo) | |
| else: | |
| print('File already downloaded') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment