python实现知乎高颜值图片爬取
作者:Leslie-x 发布时间:2023-03-11 10:35:54
标签:python,知乎,图片,爬取
导入相关包
import time
import pydash
import base64
import requests
from lxml import etree
from aip import AipFace
from pathlib import Path
百度云 人脸检测 申请信息
#唯一必须填的信息就这三行
APP_ID = "xxxxxxxx"
API_KEY = "xxxxxxxxxxxxxxxx"
SECRET_KEY = "xxxxxxxxxxxxxxxx"
# 过滤颜值阈值,存储空间大的请随意
BEAUTY_THRESHOLD = 55
AUTHORIZATION = "oauth c3cef7c66a1843f8b3a9e6a1e3160e20"
# 如果权限错误,浏览器中打开知乎,在开发者工具复制一个,无需登录
# 建议最好换一个,因为不知道知乎的反爬虫策略,如果太多人用同一个,可能会影响程序运行
以下皆无需改动
# 每次请求知乎的讨论列表长度,不建议设定太长,注意节操
LIMIT = 5
# 这是话题『美女』的 ID,其是『颜值』(20013528)的父话题
SOURCE = "19552207"
爬虫假装下正常浏览器请求
USER_AGENT = "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.5 Safari/534.55.3"
REFERER = "https://www.zhihu.com/topic/%s/newest" % SOURCE
# 某话题下讨论列表请求 url
BASE_URL = "https://www.zhihu.com/api/v4/topics/%s/feeds/timeline_activity"
# 初始请求 url 附带的请求参数
URL_QUERY = "?include=data%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Danswer%29%5D.target.content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Danswer%29%5D.target.is_normal%2Ccomment_count%2Cvoteup_count%2Ccontent%2Crelevant_info%2Cexcerpt.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Darticle%29%5D.target.content%2Cvoteup_count%2Ccomment_count%2Cvoting%2Cauthor.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Dtopic_sticky_module%29%5D.target.data%5B%3F%28target.type%3Dpeople%29%5D.target.answer_count%2Carticles_count%2Cgender%2Cfollower_count%2Cis_followed%2Cis_following%2Cbadge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Danswer%29%5D.target.content%2Crelationship.is_authorized%2Cis_author%2Cvoting%2Cis_thanked%2Cis_nothelp%3Bdata%5B%3F%28target.type%3Danswer%29%5D.target.author.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Darticle%29%5D.target.content%2Cauthor.badge%5B%3F%28type%3Dbest_answerer%29%5D.topics%3Bdata%5B%3F%28target.type%3Dquestion%29%5D.target.comment_count&limit=" + str(
LIMIT)
HEADERS = {
"User-Agent": USER_AGENT,
"Referer": REFERER,
"authorization": AUTHORIZATION
指定 url,获取对应原始内容 / 图片
def fetch_image(url):
try:
response = requests.get(url, headers=HEADERS)
except Exception as e:
raise e
return response.content
指定 url,获取对应 JSON 返回 / 话题列表
def fetch_activities(url):
try:
response = requests.get(url, headers=HEADERS)
except Exception as e:
raise e
return response.json()
处理返回的话题列表
def parser_activities(datums, face_detective):
for data in datums["data"]:
target = data["target"]
if "content" not in target or "question" not in target or "author" not in target:
continue
html = etree.HTML(target["content"])
seq = 0
title = target["question"]["title"]
author = target["author"]["name"]
images = html.xpath("//img/@src")
for image in images:
if not image.startswith("http"):
continue
image_data = fetch_image(image)
score = face_detective(image_data)
if not score:
continue
name = "{}--{}--{}--{}.jpg".format(score, author, title, seq)
seq = seq + 1
path = Path(__file__).parent.joinpath("image").joinpath(name)
try:
f = open(path, "wb")
f.write(image_data)
f.flush()
f.close()
print(path)
time.sleep(2)
except Exception as e:
continue
if not datums["paging"]["is_end"]:
return datums["paging"]["next"]
else:
return None
初始化颜值检测工具
def init_detective(app_id, api_key, secret_key):
client = AipFace(app_id, api_key, secret_key)
options = {"face_field": "age,gender,beauty,qualities"}
def detective(image):
image = str(base64.b64encode(image), "utf-8")
response = client.detect(str(image), "BASE64", options)
response = response.get("result")
if not response:
return
if (not response) or (response["face_num"] == 0):
return
face_list = response["face_list"]
if pydash.get(face_list, "0.face_probability") < 0.6:
return
if pydash.get(face_list, "0.beauty") < BEAUTY_THRESHOLD:
return
if pydash.get(face_list, "0.gender.type") != "female":
return
score = pydash.get(face_list, "0.beauty")
return score
return detective
程序入口
def main():
face_detective = init_detective(APP_ID, API_KEY, SECRET_KEY)
url = BASE_URL % SOURCE + URL_QUERY
while url is not None:
datums = fetch_activities(url)
url = parser_activities(datums, face_detective)
time.sleep(5)
if __name__ == '__main__':
main()
来源:https://www.cnblogs.com/li1992/p/10599398.html


猜你喜欢
- 假设在python中有一字典如下:x={‘a':'1,2,3', ‘b':'2,3,4'}需
- 在 MySQL 中,可以使用 ALTER TABLE 语句来添加表字段。以下是一些示例代码,可以批量添加多个字段:1 m
- 需求背景公司前端使用 Highcharts 构建图表,图表的图例支持点击显示或隐藏相应的指标。现在有需求后端需要存储用户在前端点击后显示图表
- 工欲善其事必先利其器,Pycharm 是最受欢迎的Python开发工具,它提供的功能非常强大,是构建大型项目的理想工具之一,如果能挖掘出里面
- 我们现在一般网站都是利用的MySQL数据库搭建网站的,但是在网上看到很多网友吐槽数据库连接不上的问题,现在我就结合相关资料向提出一些我个人的
- 使用python3调用wxpy模块,监控linux日志并定时发送消息给群组或好友,具体代码如下所示:#!/usr/bin/env pytho
- 正则表达式正则表达用来匹配字符串正则表达式匹配过程依次拿出表达式和文本中的字符串进行比价如果每个字符都能匹配,则匹配成功;一旦有匹配不成功的
- 作者:做梦的人(小姐姐)出处:https://www.cnblogs.com/chongyou/python读取yaml文件使用,有两种方式
- 代码示例#输入'''order_id:31489join_course[0][join_tel]:131309998
- 一、数据描述数据集中9994条数据,横跨1237天,销售额为2,297,200.8603美元,利润为286,397.0217美元,他们的库存
- 事件捕捉(Event Capture)的实现问题 W3C DOM Level2的事件模型规范中,事件在DOM树中的传播过程(从根节点到目标节
- 利用线程生成缩略图;读取当前路径下的png文件,在当前路径下生成6464,128128和32*32的缩略图。""&quo
- 一、前言Go程序像C/C++一样,如果开发编码考虑不当,会出现cpu负载过高的性能问题。如果程序是线上环境或者特定场景下出现负载过高,问题不
- 效果图实例代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit
- 最近尝试把项目迁移到Python环境下,特别新装了一台干净的Debian系统,准备重新配置环境,上网找了一些运行Python Web的环境方
- 本文实例讲述了Python过滤列表用法。分享给大家供大家参考,具体如下:过滤列表[mapping-expression for elemen
- 这是 2020 年第 3 个版本,也是最后一个版本。在 GoLand 2020.3 中,您可以探索 goroutines dumps,运行并
- 项目的一个需求是解析nginx的日志文件。简单的整理如下:日志规则描述首先要明确自己的Nginx的日志格式,这里采用默认Nginx日志格式:
- 用python连接Oracle是总是乱码,最有可能的是oracle客户端的字符编码设置不对。本人是在进行数据插入的时候总是报关键字"
- MASK图像掩膜处理在图像操作中有时候会用到掩膜处理,如果使用遍历法掩膜图像ROI区域对于python来讲是很慢的,所以我们要找到一种比较好