import json
import os
import requests
class Photo:
def __init__(self):
self.url = 'https://www.duitang.com/napi/blogv2/list/by_search/?kw=%E7%BE%8E%E5%A5%B3&after_id={}&type=feed&include_fields=top_comments%2Cis_root%2Csource_link%2Citem%2Cbuyable%2Croot_id%2Cstatus%2Clike_count%2Clike_id%2Csender%2Calbum%2Creply_count%2Cfavorite_blog_id&_type=&_=1685664035130'
self.headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.54'
}
self.num = 1
def get_data(self, ID):
response = requests.get(self.url.format(ID), headers=self.headers)
if response.status_code == 200:
return response.text
else:
return None
def parse_data(self, response):
data_list = json.loads(response)['data']['object_list']
for data in data_list:
url = data['photo']['path']
yield url
def download(self, img_url):
img = requests.get(img_url, headers=self.headers).content
with open('./美女/{}'.format(self.num) + os.path.splitext(img_url)[-1], 'wb') as f:
f.write(img)
print(f'第{self.num}张,下载成功!')
self.num += 1
def main(self):
page = int(input('请输入你要下载的总页数:'))
for i in range(page * 5):
response = self.get_data(24 * i)
for img_url in self.parse_data(response):
self.download(img_url)
if __name__ == '__main__':
photo = Photo()
photo.main()
下载 美图(Ajax)
于 2023-06-02 09:35:28 首次发布