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

一见钟情钟的不是情,是脸
日久生情生的不是脸,是情
项目简介
本项目利用Python爬虫和百度人脸识别API,针对简书交友专栏,爬取用户照片(侵删),并进行打分。
本项目包括以下内容:
- 图片爬虫
- 人脸识别API使用
- 颜值打分并进行文件归类
图片爬虫
现在各大交友网站都会有一些用户会爆照,本文爬取简书交友专栏(https://www.jianshu.com/c/bd38bd199ec6)的所有帖子,并进入详细页,获取所有图片并下载到本地。

代码
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/@data-original-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)

人脸识别API使用
由于爬取了帖子下面的所有图片,里面有各种图片(不包括人脸),而且是为了找到高颜值小姐姐,如果人工筛选费事费力,这里调用百度的人脸识别API,进行图片过滤和颜值打分。
人脸识别应用申请
- 首先,进入百度人脸识别官网(http://ai.baidu.com/tech/face),点击立即使用,登陆百度账号(没有就注册一个)。

- 创建应用,完成后,点击管理应用,就能看到AppID等,这些在调用API时需要使用的。


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

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)

颜值打分并进行文件归类
最后结合图片数据和颜值打分,设计代码,过滤掉非人物以及男性图片,获取小姐姐图片的分数(这里处理为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分以上的小姐姐很少,如图(侵删)。

最后传播一个喜大普奔的消息
腾讯云有史以来最大优惠,新用户福利1000减750!云服务器最低3折,1核1G内存50G硬盘1年最低325元!戳此了解详情!
作者:罗罗攀
链接:https://www.jianshu.com/p/7ba9c90ff12d
來源:简书
Python爬虫+颜值打分,5000+图片找到你的Mrs. Right的更多相关文章
- 5000+图片找到你喜欢的那个TA,Python爬虫+颜值打分
前言 文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 作者: 罗罗攀 PS:如有需要Python学习资料的小伙伴可以加点击下方链接 ...
- python爬虫-爬取百度图片
python爬虫-爬取百度图片(转) #!/usr/bin/python# coding=utf-8# 作者 :Y0010026# 创建时间 :2018/12/16 16:16# 文件 :spider ...
- [记录][python]python爬虫,下载某图片网站的所有图集
随笔仅用于学习交流,转载时请注明出处,http://www.cnblogs.com/CaDevil/p/5958770.html 该随笔是记录我的第一个python程序,一个爬去指定图片站点的所有图集 ...
- Python爬虫获取知乎图片
前段时间想抓点知乎问题中的图片,了解了下爬虫,发现还是Python的简单方便,于是做了点尝试. #coding=utf-8 import urllib import re def getHtml(ur ...
- Python爬虫02——贴吧图片爬虫V2.0
Python小爬虫——贴吧图片爬虫V2.0 贴吧图片爬虫进阶:在上次的第一个小爬虫过后,用了几次发现每爬一个帖子,都要自己手动输入帖子链接,WTF这程序简直反人类!不行了不行了得改进改进. 思路: 贴 ...
- Python爬虫爬取网页图片
没想到python是如此强大,令人着迷,以前看见图片总是一张一张复制粘贴,现在好了,学会python就可以用程序将一张张图片,保存下来. 今天逛贴吧看见好多美图,可是图片有点多,不想一张一张地复制粘贴 ...
- python爬虫调用搜索引擎及图片爬取实战
实战三-向搜索引擎提交搜索请求 关键点:利用搜索引擎提供的接口 百度的接口:wd="要搜索的内容" 360的接口:q="要搜索的内容" 所以我们只要把我们提交给 ...
- python爬虫模拟登录的图片验证码处理和会话维持
目标网站:古诗文网 登录界面显示: 打开控制台工具,输入账号密码,在ALL栏目中进行抓包 数据如下: 登录请求的url和请求方式 登录所需参数 参数分析: __VIEWSTATE和__VIEWSTAT ...
- python 爬虫得到网页的图片
import urllib.request,os import re # 获取html 中的内容 def getHtml(url): page=urllib.request.urlopen(url) ...
随机推荐
- [教程] NETGEAR R7800 路由器TFTP刷机方法(适用于.img格式固件各种刷)
本教程是我参照R7800的OP/LEDE固件交流群内文件做的教程,可以说是完善.补充吧. 本帖适用于:① 原厂固件刷原厂固件:② 原厂固件刷第三方固件(.img格式):③ 第三方固件刷回原厂固件(.i ...
- Mac使用技巧之Finder的个人收藏
当使Finder的时候,左側会列出来个人收藏,能够非常方便的打开对应的文件夹.那么怎样把自己新建的文件夹也增加到个人收藏呢? 1.默认的个人收藏 2.新建名字为my_ios_demo文件夹,拖动这个文 ...
- 网络最大流增广路模板(EK & Dinic)
EK算法: int fir[maxn]; int u[maxm],v[maxm],cap[maxm],flow[maxm],nex[maxm]; int e_max; int p[maxn],q[ma ...
- ios跟踪工具introspy使用
1.cydia安装introspy,前提要安装Applist (雷锋源) 2.设置中有introspy-Apps instrospy-Settings选项. 可以选择需要跟踪的app以及跟踪内 ...
- asp.net mvc 的 视图(view )的模块化开发
目前所在项目有一个视图,几个模块都涉及到,对应不同的开发人员,虽然有SVN在管理,但代码冲突时有发生.并且大家的代码挤于其中,逻辑比较容易混乱. 将不同部件独立出去,实有必要. 分离方式,我知道的有 ...
- 基于百度AI人脸识别技术的Demo
编写demo之前首先浏览官方API:http://ai.baidu.com/docs#/Face-API/top 下面是源码: package com.examsafety.test; import ...
- RPC和微服务
1 什么是RPC 是remote procedure call的缩写. 2 什么是微服务 所谓的微服务就是说,把一个应用分解成一组小的服务,每个服务运行在自己的进程中.每个服务都可以单独部署,可以用自 ...
- input title 悬浮值
<!doctype html><html lang="en"> <head> <meta charset="UTF-8&quo ...
- HDU5768Lucky7
Lucky7 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- H264--1--编码原理以及I帧B帧P帧[4]
---------------------- 前言 ----------------------- H264是新一代的编码标准,以高压缩高质量和支持多种网络的流媒体传输著称,在编码方面,我理解的他的理 ...