一. 爬虫是什么?
网络爬虫是一种按照一定的规则,自动的抓取万维网信息的程序或者脚本.
简单的说:就是用事先写好的程序去抓取网络上所需的数据.编写网络爬虫的程序员叫做爬虫工程师.
.
二. 爬虫的四个主要步骤:
1.明确目标
找到需要获取的网页url
2.爬
将所有网页上的内容都爬下来
3.取
去掉对我们没用处的数据,筛选出我们需要的数据
4.处理数据
按照我们的方式储存和使用这些数据
.
三. 爬虫的案例( 王者荣耀皮肤图片的抓取 )
1.初级版
# 导入模块
import requests
# 定义请求头,需自行添加用户代理
headers={
'User-Agent': '自己的网页上的用户代理',
}
# 定义参数
# params={}
# 请求网址 https://pvp.qq.com/web201605/js/herolist.json
# 发起请求
response=requests.get(url='https://pvp.qq.com/web201605/js/herolist.json',headers=headers)
# 获取数据
url_list = response.json()
for url in url_list:
# 获取 名称
name = url['cname']
# 获取 id
id = url['ename']
# 获取 皮肤 (预防取空报错)
try:
pifu = url['skin_name'].split('|')
except Exception as e:
print(e)
# print(pifu)
for i in range(1, len(pifu)+1):
# 请求图片地址
url = f'http://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{id}/{id}-bigskin-{i}.jpg'
response = requests.get(url=url, headers= headers)
# 保存图片到文件夹中
with open(f'./hero_imgs/{pifu[i-1]}.jpg', 'wb') as fp:
fp.write(response.content)
# print(url)
.
2.升级版(用函数编写)
# 王者荣耀皮肤抓取
# URL:https://pvp.qq.com/web201605/herolist.shtml
# 需求:获取所有英雄的所有皮肤并下载到本地
# 思路:
# 1. 请求英雄列表页URL ---> 获取每一个英雄的URL
# 2. 请求每个英雄的URL ---> 获取对应英雄的皮肤URL
# 3. 请求每个英雄的皮肤URL ---> 保存
import requests
from lxml import etree
# 发起请求,接收响应
# response = requests.get(url='https://pvp.qq.com/web201605/herolist.shtml',headers=headers)
# print(response.content.decode('gbk'))
# 夏洛特:https://pvp.qq.com/web201605/herodetail/536.shtml
# 阿古朵:https://pvp.qq.com/web201605/herodetail/533.shtml
# 蒙犽:https://pvp.qq.com/web201605/herodetail/524.shtml
# 经过对比,发现,只有数字不同,每个英雄都有对应的数字
# 所以,找每个英雄的id
# 可以通过json文件,获取每个英雄的ename,ename对应每个英雄的id
# 皮肤规律:
# 廉颇:
# 1. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/105/105-bigskin-3.jpg
# 2. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/105/105-bigskin-2.jpg
# 3. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/105/105-bigskin-1.jpg
# 赵云
# 1. //game.gtimg.cn/images/yxzj/img201606/skin/hero-info/107/107-bigskin-8.jpg
# 定义获取ename的函数
def get_ename(url):
ename_lst = []
# 发起请求,接收响应
response = requests.get(url=url,headers=headers)
content_list = response.json()
# 循环获取每一个英雄的ename
for content in content_list:
ename = content['ename']
ename_lst.append(ename)
return ename_lst
# 定义获取图片链接函数
def get_img_href(url):
# 发起请求,接收响应
response = requests.get(url=url,headers=headers)
html = etree.HTML(response.content.decode('gbk'))
# 获取皮肤数量
ul = html.xpath('//ul[@class="pic-pf-list pic-pf-list3"]/@data-imgname')[0]
pifu_list = ul.split('|')
return pifu_list
# 定义下载图片函数
def download_img(ename,pifu_list):
length = len(pifu_list)
for i in range(1,length+1):
img_response = requests.get(url=pifu_url.format(ename,ename,i))
# 保存图片
with open(pifu_list[i-1]+'.jpg','wb') as fp:
fp.write(img_response.content)
pass
# 定义主函数
def main():
ename_lst = get_ename(herolist_url)
for ename in ename_lst[0:2]:
pifu_list = get_img_href(hero_detail_url.format(ename))
download_img(ename,pifu_list)
pass
if __name__ == '__main__':
# 定义获取ename的URL
herolist_url = 'https://pvp.qq.com/web201605/js/herolist.json'
# 定义请求头字典
headers = {
'User-Agent': '自己网页上的用户代理',
}
# 定义英雄详情页的URL
hero_detail_url = 'https://pvp.qq.com/web201605/herodetail/{}.shtml'
# 定义英雄皮肤的URL
pifu_url = 'https://game.gtimg.cn/images/yxzj/img201606/skin/hero-info/{}/{}-bigskin-{}.jpg'
main()