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) ...
随机推荐
- M2Crypto
M2Crypto = Python + OpenSSL + SWIG M2Crypto is a crypto and SSL toolkit for Python. 上面是M2Crypto的READ ...
- POJ 1384 POJ 1384 Piggy-Bank(全然背包)
链接:http://poj.org/problem?id=1384 Piggy-Bank Time Limit: 1000MS Memory Limit: 10000K Total Submissio ...
- SaltStack学习系列之state常用模块
常用模块:cron,cmd,file,mount,ntp,pkg,service,user,group cmd模块 参数: name:要执行的命令 unless:用于检查的命令,只有unless指向的 ...
- Android进程间通信之内部类作为事件监听器
在Android中,使用内部类能够在当前类里面发用改监听器类,由于监听器类是外部类的内部类.所以能够自由訪问外部类的全部界面组件. 下面是一个调用系统内部类实现短信发送的一个样例: SMS类: pac ...
- Codeforces Round #320 (Div. 2) [Bayan Thanks-Round] B. Finding Team Member 排序
B. Finding Team Member ...
- http ssl
http ssl
- Swing手动进行最大化最小化
首先jdk的setExtendedState是有bug的,需要先重载JFrame的setExtendedState方法 /** * Fix the bug "jframe undecorat ...
- PSAM卡之常用APDU指令错误码【转】
本文转载自:http://blog.csdn.net/lvxiangan/article/details/53933714 PSAM卡的内容交互,是通过APDU指令完成的,常见的APDU报文格式如下: ...
- HQL 查询数据 (获取页面输入的查询条件字段)
/* * 查询提取位置表所有数据 * */ public String ListEtlExtractPositionOfAll(){ // 接受数据库中传送的code int code = Integ ...
- Linux下Redis的安装和部署 详细
一.Redis介绍 Redis是当前比较热门的NOSQL系统之一,它是一个key-value存储系统.和Memcache类似,但很大程度补偿了Memcache的不足,它支持存储的value类型相对更多 ...