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) ...
随机推荐
- [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(下)
---------------------------------------------------------------------------------------------------- ...
- jqury-validate表单验证
首先需要引入插件:jquery.validate.js这个插件. 然后对需要验证的表单实现js: $("#add-firewalls-form").validate({ submi ...
- 工作总结 for 另类写法 循环加时间 集合合并 也是用的 static class Enumerable (IEnumerable<T>的扩展方法) (IEnumerable<T> 的 工具类) (所有集合 数组都实现IEnumerable<T>)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 跨域CORS原理及调用详细演示样例
上篇博客介绍了JSONP原理,其不足,就是仅仅能使用GET提交.若传输的数据量大.这个JSONP方式就歇菜了.那这篇博客就来介绍还有一种跨域介绍方案-CORS. 相对JSONP,CORS支持P ...
- Canvas: trying to use a recycled bitmap android.graphics.Bitmap@XXX
近期在做和图片相关显示的出现了一个问题,整理一下思路.分享出来给大家參考一下: Exception Type:java.lang.RuntimeException java.lang.RuntimeE ...
- Android学习笔记-tween动画之xml实现
继上篇tween动画的java实现:http://www.cnblogs.com/fengtengfei/p/3957800.html, 这里我接着介绍一下tween动画的xml实现的方法, 首先 ...
- Android官方api的下载
不少开发者,都需要api来进行参考,可是目前Android开发来说默认是没有下载的, 而且sdk中有时候部分开发者也很疑惑怎么没有找到像document或者doc之类的文档进行下载,其实我们只要在把s ...
- 【bzoj2654]】tree
给白色边都加上一个值,二分这个值,使得选取的白边数量减少 #include<algorithm> #include<iostream> #include<cstdlib& ...
- HDU 5783Divide the Sequence
Divide the Sequence Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- android7.0 编译问题及解决【转】
本文转载自:http://blog.csdn.net/zhangmingbao2016/article/details/52699182 注意:Linux操作系统上编译Android 7.0必须使用o ...