爬取王者荣耀 和LOL


其他的我也不想多说

代码



import os,zipfile
import requests


'''
王者
'''

headers={
"Use-Agent": "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.108 Mobile Safari/537.36"
}

# 做出请求
def start_request( url):
request = requests.get(url, headers=headers)
if request.status_code == 200 :
return request.json()

def save_img( filename, content):
with open(filename, 'wb') as file:
file.write(content)


def save_zip( newpath, path):
print( "开始压缩文件")
with zipfile.ZipFile(newpath, 'w',zipfile.ZIP_DEFLATED) as zp:
for path, fileparename, filename in os.walk(path): #path fileparename filename 路劲,文件夹名,文件名
for name in filename:
zp.write(os.path.join(path,name)) # 把要压缩的文件写入压缩
print( "压缩文件成功")

if __name__ == '__main__':
url = "http://pvp.qq.com/web201605/js/herolist.json"
img = "http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/"
herojson = start_request(url)
num = 0

for i in range( len(herojson)):
hero_code = herojson[i][ 'ename']
hero_name = herojson[i][ 'cname']
hero_skinlist =herojson[i][ 'skin_name'].split( '|')

# 文件夹不存在则创建
save_dir = r 'E: \p ython \p ythonzong \爬 \r equest \i mg \% s'%hero_name
if not os.path.exists(save_dir):
os.mkdir(save_dir)

for j in range( 1, len(hero_skinlist)+ 1):
num += 1
imgurl =img + str(hero_code) + '/' + str(hero_code) + '-bigskin-'+ str(j)+ '.jpg'
# 组装imgurl
request = requests.get(imgurl, headers=headers)

filename = ' {0} \ {1} .jpg'.format(save_dir,hero_skinlist[j- 1])
save_img(filename,request.content)

print(num)
print( "爬取成功")
path= r "E: \p ython \p ythonzong \爬 \r equest \i mg"
newpath= r "E: \p ython \p ythonzong \爬 \r equest \王 者荣耀img.zip"
save_zip(newpath,path)



import json
import os

import requests

headers = { 'User-Agent': 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.7 Mobile Safari/537.36'}
num = 1

def get_headers():
'''
得到json对象
'''
url = 'http://lol.qq.com/biz/hero/champion.js'

request = requests.get(url, headers =headers)

jsontext = request.text[request.text.find( 'key')- 2:- 1] # 得到标准的json字符串

jsons = json.loads(jsontext) # 得到python对象
return jsons

'''
循坏组合成得到图片url
'''
def get_imgurl( jsons):
for valu in jsons.get( 'keys').values():
hero_url = 'http://lol.qq.com/biz/hero/'+valu+ '.js' # 得到英雄的详资源定位符

request = requests.get(hero_url, headers =headers)

jsontext = request.text[request.text.find( 'data')- 2:- 1] # 得到标准的json字符串

jsons = json.loads(jsontext) # 得到python对象
# 根据英雄创建一个文件夹
filepaths = r 'E: \p ython \p ythonAdvanced \i mgs \% s'%jsons.get( 'data').get( 'title')
if not os.path.exists(filepaths):
os.mkdir(filepaths)
for item in jsons.get( 'data').get( 'skins'):
yield {
'imgid':item.get( 'id'),
'num': item.get( 'num'),
'name':item.get( 'name'),
'filepaths':filepaths
}

def save_img( yields):
for item in yields:
imgurl = r "http://ossweb-img.qq.com/images/lol/web201310/skin/big"+ str(item.get( 'imgid')) + ".jpg"
request = requests.get(imgurl, headers =headers)

print( '爬取 {0} 英雄的皮肤中...'.format(item.get( 'name')))
filename = item.get( 'filepaths')+ " \\ " + item.get( 'name')+ ".jpg"
with open(filename, 'wb') as file:
file.write(request.content)
global num
num += 1

if __name__ == '__main__':
jsons = get_headers()
imgurls = get_imgurl(jsons)
save_img(imgurls)
print( '爬取完毕,共爬取 {0} 张英雄皮肤'.format(num))




你可能感兴趣的:(python)