前言
嗨喽,大家好呀~这里是爱看美女的茜茜呐

开发环境:
- Python 3.8
- Pycharm 2021.2
模块使用:
- requests >>> pip install requests
- tqdm >>> pip install tqdm 简单实现进度条效果
- os 文件操作
- base64
如果安装python第三方模块:
- win + R 输入 cmd 点击确定, 输入安装命令 pip install 模块名 (pip install requests) 回车
- 在pycharm中点击Terminal(终端) 输入安装命令
本次案例:
一. 采集主播照片
“”"
- 发送请求, 模拟浏览器对于url地址发送请求
伪装模拟 --> headers 请求头
字典数据类型, 要构建完整键值对
<Response [200]> 响应对象, 表示请求成功
“”"
请求链接
url = 'https://*****/cache.php?m=LiveList&do=getLiveListByPage&gameId=1663&tagAll=0&page=2'模拟浏览器
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}发送请求
response = requests.get(url=url, headers=headers)“”"
- 获取数据, 获取服务器返回响应数据
开发者工具: response
requests.exceptions.JSONDecodeError: Expecting value: line 1 column 1 (char 0)原因: 获取数据不是完整json数据格式
解决:
- 获取文本数据, 查看数据返回效果
- 通过正则表达式提取数据
删掉 请求链接 里面参数 Callback
- 解析数据, 提取我们想要的数据内容
照片url / 昵称
response.json() --> 字典数据类型
根据键值对取值 --> 根据冒号左边的内容[键], 提取冒号右边的内容[值]
“”"
for循环遍历, 一个一个提取列表里面元素
for index in response.json()['data']['datas']:提取照片
img_url = index['screenshot']提取昵称
name = index['nick']
print(name, img_url)“”"
- 保存数据 --> 需要对图片链接发送请求, 获取二进制<图片>数据
‘img\’<文件夹> + name<文件名> + ‘.jpg’<文件格式>, mode=‘wb’<二进制保存>
“”"
获取图片二进制数据
img_content = requests.get(url=img_url, headers=headers).content保存数据
with open('img\\' + name + '.jpg', mode='wb') as f:
f.write(img_content)二. 对于照片进行人脸识别检测, 进行颜值评分
使用百度云API接口
- 注册一个百度云账号
- 创建应用 --> 领取免费资源
- 点击技术文档
- Access Token获取
导入数据请求模块
–> 第三方模块, 需要安装 pip install requests
import requests
import base64
import os
import time
from tqdm import tqdmdef score(file):“”"
定义函数
:param file: 文件路径
“”"
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}client_id 为官网获取的AK, client_secret 为官网获取的SK
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=AK&client_secret=SK'
response = requests.get(host, headers=headers)
access_token = response.json()['access_token']读取一张图片数据
img_content = open(file, mode='rb').read()
base_data = base64.b64encode(img_content)
request_url = "https://aip.baidubce.com/rest/2.0/face/v3/detect"
params = {
# 图片数据
"image": base_data,
"image_type": "BASE64",
"face_field": "beauty"
}
request_url = request_url + "?access_token=" + access_token
headers_1 = {'content-type': 'application/json'}
json_data = requests.post(request_url, data=params, headers=headers_1).json()
try:
num = json_data['result']['face_list'][0]['beauty']
return num
except:
return '识别失败'info_list = []对于所有照片进行颜值检测 --> 获取文件路径/文件名字
files = os.listdir('img\\')
print('正在做颜值评分, 请稍后.....')
for file in tqdm(files):
# 延时请求慢点
time.sleep(0.5)
# 完整的路径
filename = 'img\\' + file
# 切片
name = file[:-4]
result = score(file=filename)
if result != '识别失败':
dit = {
'主播': name,
'颜值': result
}
# 列表添加元素
info_list.append(dit)
info_list.sort(key=lambda x:x['颜值'], reverse=True)
i = 1
for info in info_list:
print(f'颜值排名第{i}的是{info["主播"]}, 颜值评分是{info["颜值"]}')
i += 1三. 评分排名
检测得对照标准:




















