教你用Python采集阴阳师式神全图鉴图片

导语

本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,如有问题请及时联系我们以作处理。

基本环境配置

  • python 3.6

  • pycharm

  • requests

目标网页

教你用Python采集阴阳师式神全图鉴图片_第1张图片


经过一些系列的网页分析,可以发现它的图片地址

https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_big_beforeAwake/201.png
https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_big_afterAwake/201.png
https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_skin/201-1.png

教你用Python采集阴阳师式神全图鉴图片_第2张图片


多看几张式神的图片后可以发现,每个图片所改变的是式神的 ID 值

可以通过开发者工具抓包,找到数据接口

教你用Python采集阴阳师式神全图鉴图片_第3张图片

请求数据

  • 获得json数据

import requests
url = 'https://yys.res.netease.com/pc/zt/20161108171335/js/app/all_shishen.json'
headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
response = requests.get(url=url, headers=headers)
html_data = response.json()

解析数据

  • 获取 id 值 以及 式神名字

  • 构建图片地址

for i in html_data:
    num_id = i['id']
    name = i['name']
    beforeAwake = 'https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_big_beforeAwake/{}.png'.format(num_id)
    afterAwake = 'https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_big_afterAwake/{}.png'.format(num_id)
    skin = 'https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_skin/{}-1.png'.format(num_id)

    beforeAwake_response = requests.get(url=beforeAwake, headers=headers)
    beforeAwake_name = name + '初始' + '.png'

    afterAwake_response = requests.get(url=beforeAwake, headers=headers)
    afterAwake_name = name + '觉醒' + '.png'

    skin_response = requests.get(url=beforeAwake, headers=headers)
    skin_name = name + '皮肤' + '.png'

保存数据

path = 'D:\\python\\demo\\阴阳师\\img\\'
def download(name, response):
    with open(path + name, mode='wb') as f:
        f.write(response.content)

完整代码

import requests
import pprint
url = 'https://yys.res.netease.com/pc/zt/20161108171335/js/app/all_shishen.json'
path = 'D:\\python\\demo\\阴阳师\\img\\'

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36'
}
def download(name, response):
    with open(path + name, mode='wb') as f:
        f.write(response.content)

response = requests.get(url=url, headers=headers)
html_data = response.json()
for i in html_data:
    num_id = i['id']
    name = i['name']

    beforeAwake = 'https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_big_beforeAwake/{}.png'.format(num_id)
    afterAwake = 'https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_big_afterAwake/{}.png'.format(num_id)
    skin = 'https://yys.res.netease.com/pc/zt/20161108171335/data/shishen_skin/{}-1.png'.format(num_id)

    beforeAwake_response = requests.get(url=beforeAwake, headers=headers)
    beforeAwake_name = name + '初始' + '.png'
    download(beforeAwake_name, beforeAwake_response)

    afterAwake_response = requests.get(url=beforeAwake, headers=headers)
    afterAwake_name = name + '觉醒' + '.png'
    download(afterAwake_name, afterAwake_response)

    skin_response = requests.get(url=beforeAwake, headers=headers)
    skin_name = name + '皮肤' + '.png'
    download(skin_name, skin_response)
    print('正在下载{}图鉴'.format(name))

教你用Python采集阴阳师式神全图鉴图片_第4张图片

教你用Python采集阴阳师式神全图鉴图片_第5张图片

教你用Python采集阴阳师式神全图鉴图片_第6张图片

教你用Python采集阴阳师式神全图鉴图片_第7张图片

教你用Python采集阴阳师式神全图鉴图片_第8张图片

 end

这次分享到这就结束啦~喜欢的话记得给小编个三连再走啦!家人们的支持是小编更新最大的动力

想领取更多完整源码更Python学习资料可点击这行字体哦

你可能感兴趣的:(程序员,Python,python,爬虫,开发语言)