01 requests基础
1.请求网络数据
response = requests.get('https://cd.zu.ke.com/zufang')
2.设置解码方式(一定要在获取请求前设置)
response.encoding = 'gb2312'
3.获取请求结果
3.1 获取请求结果对应的文本数据 - 爬网页
print(response.text)
3.2 获取二进制格式的请求结果 - 下载图片、视频、音频
print(response.content)
3.3 获取请求结果json转换的结果 - json接口
print(response.json())
02 添加Header
1.发送请求
添加header:a、浏览器伪装(user-agent);b、免密登陆(cookie);c、设置代理(proxies)
headers = {
# 'cookie': ''
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36'
}
response = requests.get('https://movie.douban.com/top250', headers=headers)
2.获取结果
result = response.text
print(result)
03 下载图片数据
1.获取网络图片数据
response = requests.get('lol.qq.com/data/info-heros.shtml')
result = response.content
print(type(result))
2.保存图片数据到本地文件夹
with open('files/a.jpg', 'wb') as f:
f.write(result)
04 os模块
import os
# os.mkdir(文件路径) – 在指定的位置创建指定文件夹
# os.mkdir('files/test') # 如果文件夹已经存在会报错!
# os.path.exists(文件夹路径/文件路径) - 判断指定文件夹或文件是否存在,返回True、False
if not os.path.exists('files/test'):
os.mkdir('files/test')
05 requests作业
拔取所有英雄的皮肤图片并保存到本地
import requests, os
def get_heros_skins(heros_list_js):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.5359.95 Safari/537.36'
}
response = requests.get(heros_list_js, headers=headers)
result = response.json()
for i in result['hero']:
heroId = i['heroId']
response2 = requests.get(rf'https://game.gtimg.cn/images/lol/act/img/js/hero/{heroId}.js', headers=headers)
result2 = response2.json()
heroId2 = f'{i["heroId"]:0>4}'
heroName2 = i['name']
heroDirs = heroId2 + heroName2
dirs_path = rf'./HeroSkins/{heroDirs}'
if not os.path.exists(dirs_path):
os.makedirs(rf'./HeroSkins/{heroDirs}')
print(f'{heroName2}文件夹创建成功!')
for j in result2['skins']:
heroName = j['heroName']
heroSkinId = j['skinId']
heroSkinName = j['name']
heroSkinImg = j['mainImg']
if len(heroSkinImg) != 0:
response3 = requests.get(heroSkinImg, headers=headers)
result3 = response3.content
if heroName2 == heroName:
heroSkinName2 = str(heroSkinId+heroSkinName)
if '/' in heroSkinName2:
heroSkinName2 = heroSkinName2.replace('/', '_')
else:
pass
with open(rf'HeroSkins/{heroDirs}/{heroSkinName2}.jpg', 'wb') as f:
f.write(result3)
else:
pass
else:
pass
else:
print('英雄皮肤文件拔取完成!')
heros_list_js = r'https://game.gtimg.cn/images/lol/act/img/js/heroList/hero_list.js'
get_heros_skins(heros_list_js)