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) ...
随机推荐
- hdu 4291 矩阵幂 循环节
http://acm.hdu.edu.cn/showproblem.php?pid=4291 凡是取模的都有循环节-----常数有,矩阵也有,并且矩阵的更奇妙: g(g(g(n))) mod 109 ...
- Qt实现Windows远程控制
实现方式为server端+client. server端为一个进程.client为图形界面程序. client连接时设置server端的Ip地址,以及须要显示的长度和宽度(不能超过相应server端显 ...
- Python循环定时服务功能(相似contrab)
Python实现的循环定时服务功能.类似于Linux下的contrab功能.主要通过定时器实现. 注:Python中的threading.timer是基于线程实现的.每次定时事件产生时.回调完响应函数 ...
- JavaSE入门学习5:Java基础语法之keyword,标识符,凝视,常量和变量
一keyword keyword概述:Java语言中有一些具有特殊用途的词被称为keyword.keyword对Java的编译器有着特殊的意义.在程 序中应用时一定要谨慎. keyword特点:组成k ...
- hbase shell经常使用命令
hbase经常使用命令 /usr/local/cloud/hbase/bin/hbase shell 用shell来连接hbase exit 退出hbase shell version 查看hbase ...
- android中怎么将桌面较长的图标名称显示完整
找到相应的 res 资源, 改动其 styles.xml <style name="WorkspaceIcon.Portrait"> <item ...
- make常见报错原因分析
1 No rule to make target ‘xxx’ 原因一般是'xxx'文件不存在,首先应该去相应的目录检查文件是否存在.
- qq xxs
qq xxs qq xxs 有很多举个简单例子 : QQ空间发段文章 带上 <script> 提交(document.cookie) 到 自己服务端获取 </script> 在 ...
- JavaScript全局属性/函数
JavaScript 全局属性和方法可用于创建Javascript对象. JavaScript 全局属性 属性 描述 Infinity 代表正的无穷大的数值. NaN 指示某个值是不是数字值. und ...
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation
C. Fox and Box Accumulation time limit per test 1 second memory limit per test 256 megabytes input s ...