import requests
import jsonpath
import json
import urllib.parse
import os
#目标url
url = "https://www.duitang.com/napi/blog/list/by_search/?kw={}&start={}"
label = input("请输入关键词:")
label = urllib.parse.quote(label)
page = input("获取页数:")
page = int(page) * 24 + 1
print("开始获取>>>")
#创建文件夹
b=os.getcwd()
flag = os.path.exists(b + '//jpgHome')
if(flag==False):
path = os.mkdir(b+'//jpgHome')
num = 0
for index in range(0,page,24):
urls = url.format(label,index)
print("图片地址:"+urls)
resp = requests.get(urls)
we_data = resp.text # 获取网页的文本
html = json.loads(we_data)
photos = jsonpath.jsonpath(html, "$..path")
# 保存数据
for i in photos:
a = requests.get(i)
with open(r'jpgHome/{}.jpg'.format(num), 'wb') as f:
f.write(a.content)
num += 1
print("执行结束>>>")