forked from delta4d/douban-album-dl
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: wujiawei <wujiawei@xiaomi.com>
- Loading branch information
wujiawei
committed
Aug 4, 2017
1 parent
06c38e6
commit 3ba69c4
Showing
4 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
#!/usr/bin/env python3 | ||
# -*- coding:utf-8 -*- | ||
from bs4 import BeautifulSoup | ||
import requests | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
import os | ||
|
||
import requests | ||
from bs4 import BeautifulSoup | ||
|
||
from utils import file_utils | ||
|
||
|
||
class Movie: | ||
BASE_URL = 'https://movie.douban.com/subject/{}/photos' | ||
|
||
def __init__(self, movie_id): | ||
self.url = Movie.BASE_URL.format(movie_id) | ||
|
||
def photos(self): | ||
start = 0 | ||
while True: | ||
next_photos = self.__photos(start) | ||
step = len(next_photos) | ||
if 0 == step: | ||
break | ||
for photo in next_photos: | ||
yield photo.a['href'], photo.img["src"].replace("photo/thumb", "photo/raw") | ||
start += step | ||
|
||
def __photos(self, start): | ||
url = self.url | ||
params = { | ||
'type': 'S', | ||
'start': start, | ||
'sortby': 'like', | ||
'size': 'a', | ||
'subtype': 'a' | ||
} | ||
headers = { | ||
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36' | ||
} | ||
r = requests.get(url, params=params, headers=headers) | ||
soup = BeautifulSoup(r.text, "html.parser") | ||
return soup.find_all("div", class_="cover") | ||
|
||
|
||
def get_movie_by_id(mid, path): | ||
idx = 0 | ||
file_utils.mkdir(path) | ||
m = Movie(mid) | ||
for refer, photo_url in m.photos(): | ||
name = os.path.basename(photo_url) | ||
full_path = path + '/' + name | ||
if os.path.exists(full_path): | ||
print('pic {} exist skip'.format(name)) | ||
continue | ||
print('{}: saving {}'.format(idx, name)) | ||
headers = { | ||
'Referer': refer, | ||
"User-Agent": 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36' | ||
} | ||
file_utils.save_from_url(photo_url, headers, full_path) | ||
idx += 1 | ||
print('saving movie photos to {}'.format(path)) | ||
|
||
|
||
if __name__ == '__main__': | ||
get_movie_by_id('4191644', '/home/mi/Pictures/419') |