zoukankan      html  css  js  c++  java
  • 总看一个主播多没意思,用python给女主播做一个颜值排名,只看颜值高的!

     

    众所周知,能去直播的女主播一般都还可以,但是两三个还能肉眼分辨看看谁好看,几百个可就看不过来了吧。

    那这时候python的用处就来了,对她们的照片进行自动分析打分,给她们做一个排行榜,哪个好看一目了然~

    在这里插入图片描述

    获取照片

    既然要做排名打分,那么就得先准备图片,第一步先获取女主播图片。

    1、分析目标网页

    确定爬取的url路径,headers参数。

    base_url = 'https://www.***.com/g/4079'
    # 把**改成某动物直播的拼音
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36'}

    2、发送请求 
    requests 模拟浏览器发送请求,获取响应数据。

    response = requests.get(url=base_url, headers=headers)
    html_data = response.text

    3、解析数据

    parsel 转化为Selector对象,Selector对象具有xpath的方法,能够对转化的数据进行处理。

    parse = parsel.Selector(html_data)
    data_list = parse.xpath('//li[@class="game-live-item"]')
    # print(data_list)
    
    for data in data_list:
        img_url = data.xpath('./a/img/@data-original').get()  # 主播人脸图片的url地址
        img_title = data.xpath('.//span/i/@title').get()  # 主播的名字
        print(img_url, img_title)

    4、保存数据

    # 请求图片数据
    img_data = requests.get(url=img_url, headers=headers).content
    
    # 准备文件名
    file_name = img_title + '.jpg'
    with open('img\\' + file_name, mode='wb') as f:
        print('正在保存:', file_name)
        f.write(img_data)

    在这里插入图片描述

    颜值检测

    调用人脸接口,进行颜值检测,没有的话大家自己弄一下,我就不提供了。
    在这里插入图片描述

    如果文章看不过瘾,我给大家准备了对应的视频讲解,Python对女主播进行颜值打分,实现排名

    def face_rg(file_Path):
        """ 你的 api_id AK SK """
        api_id = '19895116'
        api_key = 'aR6Oytn7ycDyhjBqPYCAGgqh'
        secret_key = 'dXjz8QELSaj238GuEr0I3xnarEurWhit'
    
        client = AipFace(api_id, api_key, secret_key)  # 调用颜值检测的接口(实例化对象)
    
        with open(file_Path, 'rb') as file:
             data = base64.b64encode(file.read())  # 图片类型 BASE64:图片的base64值,base64编码后的图片数据
    
        image = data.decode()
    
        imageType = "BASE64"
        options = {}
        options["face_field"] = 'beauty'
    
        """ 调用人脸检测 """
        result = client.detect(image, imageType, options)
        # print(result)
        return result['result']['face_list'][0]['beauty']
    
    
    if __name__ == '__main__':
        face_rg(r'C:\Users\admin\Desktop\练习\主播颜值检测\img\9c-专属男友.jpg')
    # 我还给大家准备了这些资料,直接在群里就可以免费领取了。
    # 群:924040232
    # python学习路线汇总
    # 精品Python学习书籍100本
    # Python入门视频合集
    # Python实战案例
    # Python面试题
    # Python相关软件工具/pycharm永久激活

    四、颜值打分

    万事具备只欠东风,打分的东风。

    import os
    
    from 主播颜值检测.颜值检测_接口 import face_rg
    
    
    path = './img'
    image_list = os.listdir(path)
    # print(image_list)
    
    score_dict = {}
    
    for image in image_list:
        try:
            name = image.split('.')[0]
            # print(name)
            image_path = path + '\\' + image  # 图片的路径
            face_score = face_rg(image_path)
            # print(face_score)
            score_dict[name] = face_score
            # print(score_dict)
        except Exception as e:
            print('正在检测:{}|检测失败!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'.format(str(name)))
        else:
            print('正在检测:{}|颜值打分为:{}'.format(str(name), str(face_score)))
    
    print('\n===========================================检测完成===========================================')
    
    print(score_dict.items())
    
    # 字典根据值降序排列
    change_score = sorted(score_dict.items(), key=lambda x: x[1], reverse=True)  # lambda中的1是元组的索引  x表示参数  x[1]表示返回值
    print(change_score)
    
    # 数据输出
    # enumerate枚举  enumerate多用于在for循环中得到计数,利用它可以同时获得索引和值,即需要index和value值的时候可以使用enumerate
    print(list(enumerate(change_score)))
    for a, b in enumerate(change_score):
        print('小姐姐的名字是:{}丨颜值名次是:第{}名丨她的颜值分数为:{}'.format(change_score[a][0], a+1, change_score[a][1]))

    颜值打分
    在这里插入图片描述
    颜值排行
    在这里插入图片描述

    哎,到这里就成功了,兄弟们可以去试试。

    记得点赞三连哇,爱你哟~

  • 相关阅读:
    Error from server at http://127.0.0.1:8983/solr/xxx: undefined field type
    js判断json对象是否为空
    js删除json指定元素
    crossdomain.xml跨域配置文件的安全注意事项
    WEB安全番外第一篇--其他所谓的“非主流”漏洞:URL跳转漏洞与参数污染
    Linux应急响应思路详谈
    glassfish任意文件读取漏洞解析
    SRC常见WEB漏洞系列之HTTP-HOST头攻击
    WEB安全第七篇--终结篇考验逻辑思维:逻辑漏洞大汇总(越权、会话逻辑、业务逻辑、暴力破解)
    WEB安全第六篇--千里之外奇袭客户端:XSS和HTML注入
  • 原文地址:https://www.cnblogs.com/hahaa/p/15797913.html
Copyright © 2011-2022 走看看