From 5eea3ee622824419cbdcb0974719ec7266b43ea7 Mon Sep 17 00:00:00 2001 From: Ein Verne Date: Tue, 14 Jul 2020 09:17:31 +0800 Subject: [PATCH] Fix album download failed closes #3 --- douban/album.py | 3 +-- utils/file_utils.py | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/douban/album.py b/douban/album.py index 912e273..12fdca8 100644 --- a/douban/album.py +++ b/douban/album.py @@ -2,7 +2,6 @@ # -*- coding:utf-8 -*- import os -import requests from bs4 import BeautifulSoup from douban import threadPoolExecutor @@ -29,7 +28,7 @@ def photos(self): def __photos(self, start): url = self.url + str(start) - r = requests.get(url) + r = file_utils.request_with_headers(url) soup = BeautifulSoup(r.text, "html.parser") return soup.find_all("div", class_="photo_wrap") diff --git a/utils/file_utils.py b/utils/file_utils.py index 9b7fb73..4f612cb 100644 --- a/utils/file_utils.py +++ b/utils/file_utils.py @@ -35,3 +35,12 @@ def get_raw_url(thumb_url): if 'photo/lthumb' in thumb_url: thumb_url = thumb_url.replace("photo/lthumb", "photo/large") return thumb_url + + +headers = { + 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36' +} + + +def request_with_headers(url): + return requests.get(url, headers=headers) \ No newline at end of file