通过网页api接口获取网页数据

鼠标右击检查

 点击network

此时无数据,Ctrl+R刷新

找到api接口

直接使用request获取json信息,将所需要信息提取出来

贴代码👇

import os
import sys
import json
import time
import urllib
import requests


def output(url, dir_name):
        request = urllib.request.Request(url=url, headers=headers)
        time.sleep(5)
        response = urllib.request.urlopen(request)
        content = response.read().decode('utf-8')
        dict_data = json.loads(content)
        img_data = dict_data.get("data")
        list_img = list(img_data)
        for i in range(len(list_img)):
                tmp_dic = list_img[i]
                tmp_collectionId = tmp_dic.get("collectionId")
                tmp_name = tmp_dic.get("name")
                tmp_artist = tmp_dic.get("artist")
                tmp_url = tmp_dic.get("imgUrl")
                suffix = str(tmp_url)[0:str(tmp_url).index("?")].split('.')[-1]
                print(str(tmp_collectionId) + "-" + tmp_name + "-" + tmp_artist + "." + suffix)
                print(tmp_url)
                res_img = requests.get(tmp_url)
                with open(os.path.dirname(sys.argv[0]) + "/" + dir_name + "/" + str(
                        tmp_collectionId) + "-" + tmp_name + "-" + tmp_artist + "." + suffix, mode='wb') as file:
                        file.write(res_img.content)


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36'
}
url1 = "https://......."  #目标网址
output(url1, "目标文件夹名")