使用python抓取王者荣耀中各个英雄的皮肤图片(二十)

2019-07-31  本文已影响0人  梦捷者

1、首先进行确定url和发送相应的请求

def start_func():
    url = 'https://pvp.qq.com/web201605/js/herolist.json'
    response = requests.get(url, headers=headers)
    print(response.text)
    print(type(response.text)) #json字符串
    html = response.json()#把响应体内容转化为json对象,即变成列表
    return html

2、解析网页和保存相应的皮肤图片

def main(html):
    heroname = list(map(lambda x: x['cname'], html))
    heronumber = list(map(lambda x: x['ename'], html))
    ii = 0
    for v in heronumber:
        os.mkdir('C:\\Users\\Lenovo\\Desktop\\wangzhe\\'+heroname[ii])
        #进入到刚刚创建的目录里面
        os.chdir('C:\\Users\\Lenovo\\Desktop\\wangzhe\\'+heroname[ii])
        #确定皮肤编号

        for u in range(0, 8):
            hero_link = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'+str(v)+'/'+str(v)+'-bigskin-'+str(u)+'.jpg'
            response = requests.get(hero_link, headers=headers)
            if response.status_code == 200:
                print('这是%s的皮肤图片链接:%s'% (heroname[ii], hero_link))
                f = open(str(u)+'.jpg', 'wb')
                f.write(response.content)
                f.close()
        ii += 1

具体简单代码如下:

import os #用代码创建文件夹
import requests

headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}


def start_func():
    url = 'https://pvp.qq.com/web201605/js/herolist.json'
    response = requests.get(url, headers=headers)
    print(response.text)
    print(type(response.text)) #json字符串
    html = response.json()#把响应体内容转化为json对象,即变成列表
    return html


def main(html):
    heroname = list(map(lambda x: x['cname'], html))
    heronumber = list(map(lambda x: x['ename'], html))
    ii = 0
    for v in heronumber:
        os.mkdir('C:\\Users\\Lenovo\\Desktop\\wangzhe\\'+heroname[ii])
        #进入到刚刚创建的目录里面
        os.chdir('C:\\Users\\Lenovo\\Desktop\\wangzhe\\'+heroname[ii])
        #确定皮肤编号

        for u in range(0, 8):
            hero_link = 'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/'+str(v)+'/'+str(v)+'-bigskin-'+str(u)+'.jpg'
            response = requests.get(hero_link, headers=headers)
            if response.status_code == 200:
                print('这是%s的皮肤图片链接:%s'% (heroname[ii], hero_link))
                f = open(str(u)+'.jpg', 'wb')
                f.write(response.content)
                f.close()
        ii += 1


if __name__ == '__main__':
    data = start_func()
    main(data)


上一篇下一篇

猜你喜欢

热点阅读