Python爬虫+颜值打分,5000+图片找到你的Mrs. Right

简介: 一见钟情钟的不是情,是脸日久生情生的不是脸,是情项目简介本项目利用Python爬虫和百度人脸识别API,针对简书交友专栏,爬取用户照片(侵删),并进行打分。
img_9df8a948e7bc58c5571ad103635877e5.png

一见钟情钟的不是情,是脸
日久生情生的不是脸,是情

项目简介

本项目利用Python爬虫和百度人脸识别API,针对简书交友专栏,爬取用户照片(侵删),并进行打分。
本项目包括以下内容:

  • 图片爬虫
  • 人脸识别API使用
  • 颜值打分并进行文件归类

图片爬虫

现在各大交友网站都会有一些用户会爆照,本文爬取简书交友专栏(https://www.jianshu.com/c/bd38bd199ec6)的所有帖子,并进入详细页,获取所有图片并下载到本地。

img_dc825397cc828c773fe6b1f470f9ff43.png
代码
import requests
from lxml import etree
import time

headers = {
    'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36'
}

def get_url(url):
    res = requests.get(url,headers=headers)
    html = etree.HTML(res.text)
    infos = html.xpath('//ul[@class="note-list"]/li')
    for info in infos:
        root = 'https://www.jianshu.com'
        url_path = root + info.xpath('div/a/@href')[0]
        # print(url_path)
        get_img(url_path)
    time.sleep(3)

def get_img(url):
    res = requests.get(url, headers=headers)
    html = etree.HTML(res.text)
    title = html.xpath('//div[@class="article"]/h1/text()')[0].strip('|').split(',')[0]
    name = html.xpath('//div[@class="author"]/div/span/a/text()')[0].strip('|')
    infos = html.xpath('//div[@class = "image-package"]')
    i = 1
    for info in infos:
        try:
            img_url = info.xpath('div[1]/div[2]/img/@src')[0]
            print(img_url)
            data = requests.get('http:' + img_url,headers=headers)
            try:
                fp = open('row_img/' + title + '+' + name + '+' + str(i) + '.jpg','wb')
                fp.write(data.content)
                fp.close()
            except OSError:
                fp = open('row_img/' + name + '+' + str(i) + '.jpg', 'wb')
                fp.write(data.content)
                fp.close()
        except IndexError:
            pass
        i = i + 1

if __name__ == '__main__':
    urls = ['https://www.jianshu.com/c/bd38bd199ec6?order_by=added_at&page={}'.format(str(i)) for i in range(1,201)]
    for url in urls:
        get_url(url)
img_3a6e4ce50b216c0eae59ff7f4b658b15.png

人脸识别API使用

由于爬取了帖子下面的所有图片,里面有各种图片(不包括人脸),而且是为了找到高颜值小姐姐,如果人工筛选费事费力,这里调用百度的人脸识别API,进行图片过滤和颜值打分。

人脸识别应用申请
  • 首先,进入百度人脸识别官网(http://ai.baidu.com/tech/face),点击立即使用,登陆百度账号(没有就注册一个)。
img_d66c5387837055daa574ef136f1ab162.png
  • 创建应用,完成后,点击管理应用,就能看到AppID等,这些在调用API时需要使用的。
img_0250d206e3e115eb8792385ffebd198a.png
img_8808b479f3e4dd3a7a9457cd5f895c80.png
API调用

这里使用杨超越的图片先试下水。通过结果,可以看到75分,还算比较高了(自己用了一些网红和明星测试了下,分数平均在80左右,最高也没有90以上的)。

img_e3c02991b7465b8b964b180bcaaa0624.jpe
from aip import AipFace
import base64
 
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
 
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)
 
filePath = r'C:\Users\LP\Desktop\6.jpg'
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        content = base64.b64encode(fp.read())
        return content.decode('utf-8')
    
imageType = "BASE64"
    
options = {}
options["face_field"] = "age,gender,beauty"

result = aipFace.detect(get_file_content(filePath),imageType,options)
print(result)
img_0730cf8a8738caca4bb2abba8e105fd6.png

颜值打分并进行文件归类

最后结合图片数据和颜值打分,设计代码,过滤掉非人物以及男性图片,获取小姐姐图片的分数(这里处理为1-10分),并分别存在不同的文件夹中。

from aip import AipFace
import base64
import os
import time

APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
 
aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        content = base64.b64encode(fp.read())
        return content.decode('utf-8')
    
imageType = "BASE64"
    
options = {}
options["face_field"] = "age,gender,beauty"

file_path = 'row_img'
file_lists = os.listdir(file_path)
for file_list in file_lists:
    result = aipFace.detect(get_file_content(os.path.join(file_path,file_list)),imageType,options)
    error_code = result['error_code']
    if error_code == 222202:
        continue
        
    try:
        sex_type = result['result']['face_list'][-1]['gender']['type']
        if sex_type == 'male':
            continue
    #     print(result)
        beauty = result['result']['face_list'][-1]['beauty']
        new_beauty = round(beauty/10,1)
        print(file_list,new_beauty)
        if new_beauty >= 8:
            os.rename(os.path.join(file_path,file_list),os.path.join('8分',str(new_beauty) +  '+' + file_list))
        elif new_beauty >= 7:
            os.rename(os.path.join(file_path,file_list),os.path.join('7分',str(new_beauty) +  '+' + file_list))
        elif new_beauty >= 6:
            os.rename(os.path.join(file_path,file_list),os.path.join('6分',str(new_beauty) +  '+' + file_list))
        elif new_beauty >= 5:
            os.rename(os.path.join(file_path,file_list),os.path.join('5分',str(new_beauty) +  '+' + file_list))
        else:
            os.rename(os.path.join(file_path,file_list),os.path.join('其他分',str(new_beauty) +  '+' + file_list))
        time.sleep(1)
    except KeyError:
        pass
    except TypeError:
        pass

最后结果8分以上的小姐姐很少,如图(侵删)。

img_dcb759da8736ca133dff07e35f344a43.png

讨论

  • 简书交友小姐姐数量较少,读者可以去试试微博网红或知乎美女。
  • 虽然这是一个看脸的时代,但喜欢一个人,始于颜值,陷于才华,忠于人品(最后正能量一波,免得被封)。
相关文章
|
9天前
|
数据采集 Web App开发 数据安全/隐私保护
实战:Python爬虫如何模拟登录与维持会话状态
实战:Python爬虫如何模拟登录与维持会话状态
|
1月前
|
数据采集 Web App开发 自然语言处理
新闻热点一目了然:Python爬虫数据可视化
新闻热点一目了然:Python爬虫数据可视化
|
28天前
|
数据采集 监控 数据库
Python异步编程实战:爬虫案例
🌟 蒋星熠Jaxonic,代码为舟的星际旅人。从回调地狱到async/await协程天堂,亲历Python异步编程演进。分享高性能爬虫、数据库异步操作、限流监控等实战经验,助你驾驭并发,在二进制星河中谱写极客诗篇。
Python异步编程实战:爬虫案例
|
29天前
|
数据采集 存储 XML
Python爬虫技术:从基础到实战的完整教程
最后强调: 父母法律法规限制下进行网络抓取活动; 不得侵犯他人版权隐私利益; 同时也要注意个人安全防止泄露敏感信息.
564 19
|
15天前
|
数据采集 存储 JSON
Python爬虫常见陷阱:Ajax动态生成内容的URL去重与数据拼接
Python爬虫常见陷阱:Ajax动态生成内容的URL去重与数据拼接
|
1月前
|
机器学习/深度学习 编解码 Python
Python图片上采样工具 - RealESRGANer
Real-ESRGAN基于深度学习实现图像超分辨率放大,有效改善传统PIL缩放的模糊问题。支持多种模型版本,推荐使用魔搭社区提供的预训练模型,适用于将小图高质量放大至大图,放大倍率越低效果越佳。
190 3
|
19天前
|
数据采集 存储 JavaScript
解析Python爬虫中的Cookies和Session管理
Cookies与Session是Python爬虫中实现状态保持的核心。Cookies由服务器发送、客户端存储,用于标识用户;Session则通过唯一ID在服务端记录会话信息。二者协同实现登录模拟与数据持久化。
|
1月前
|
机器学习/深度学习 文字识别 Java
Python实现PDF图片OCR识别:从原理到实战的全流程解析
本文详解2025年Python实现扫描PDF文本提取的四大OCR方案(Tesseract、EasyOCR、PaddleOCR、OCRmyPDF),涵盖环境配置、图像预处理、核心识别与性能优化,结合财务票据、古籍数字化等实战场景,助力高效构建自动化文档处理系统。
395 0
|
1月前
|
数据采集 存储 Web App开发
处理Cookie和Session:让Python爬虫保持连贯的"身份"
处理Cookie和Session:让Python爬虫保持连贯的"身份"
|
1月前
|
数据采集 Web App开发 前端开发
处理动态Token:Python爬虫应对AJAX授权请求的策略
处理动态Token:Python爬虫应对AJAX授权请求的策略

推荐镜像

更多