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
0
投稿
猜你喜欢
- 下面就来介绍一下这些在后台辛勤工作的进程们。系统检测器(System Monitor,SMON)、进程监视器(Process Monitor
- 我们需要将【小组销量排名表.xlsx】通过邮件发送给【组长邮箱.xlsx】中的各个组长。这里会学一个新的知识点—&
- 在软件开发过程中经常会遇到数据库升迁的问题,原因比较多,如acsess访问速度比sql server慢、删除数据记录后access会留下空档
- 三天前,你说下面的图是 PS 的,我信。而今天,这的的确确是张截图 -- 是的,这已经 不是梦想,是现实 -- 但实现梦想的不是微软,是 G
- 前阵子刚完成一个B/S架构的学校办公系统,体会就是表太多,文件太多,而每个文件中类似的操作(代码)也太多了,例如学生信息和教师信息操作,st
- 之前用Crystal做了一个数字转English Word的Formula刚刚心血来潮, 大半个晚上写了JS版本的数字转换, 由于JS的Bu
- 看了群主最后成像的图片,应该是循环了36次画方框,每次有10度的偏移。当然不能提前看答案,自己试着写代码。之前有用过海龟画图来画过五角星、奥
- 在很多企业会使用闲置的 Windows 机器作为临时服务器,有时候我们想远程调用里面的程序或查看日志文件Windows 内置的服务
- 本文以实例详解了python的迭代器与生成器,具体如下所示:1. 迭代器概述: 迭代器是访问集合元素的一种方式。迭代器对象从集合的
- 注:我指一个网站被第三方网站以iframe的形式调用时,被调用网站的禁止策略 和 调用网站的突破禁止策略,跟XSS麽关系,但跟clickja
- PHP crc32() 函数实例输出 crc32() 的结果:<?php $str = crc32("Hello World
- 这篇文章主要介绍了一种简单的MySQL数据库安装方法,详细内容请大家参考下文:虽然安装MySQL数据库的文章很多,但是我看后感觉对于初学者来
- 我有个MM在网上面安了家,想做一个关于特效的网站。她虽然懂一点网页制作,但是她的机器配置比较低,有时为了反复试验页面上一些特殊效果,而打开D
- 需求我的需求是批量裁剪某一文件夹下的所有图片,并指定裁剪宽高。思路1、 先使用PIL.Image.size获取输入图片的宽高。2、宽高除以2
- 对于值传递和引用传递,书本上的解释比较繁琐,而php面试中总会出现,下面我会通过一个生活的例子带大家理解它们之间区别。第一步假设我们去酒店订
- Windows 8 终于发布了,虽然现在可用的只是开发者预览版,好消息是,IE 10 也随着发了,虽然现在还只有Windows 8可用。我们
- 本文实例讲述了php面象对象数据库操作类。分享给大家供大家参考。具体实现代码如下://此处构造一个数据库操作类,封装所有数据库操作 //可以
- 因文件格式要求,需要将docx 与doc文件相互转换,特寻找python代码,与大家共分享from win32com import clie
- Embedding词嵌入在 pytorch 中非常简单,只需要调用 torch.nn.Embedding(m, n) 就可以了,m 表示单词
- MySQL是一个小型关系型数据库管理系统,开发者为瑞典MySQLAB公司,在2008年1月16号被Sun公司收购。MySQL被广泛地应用在I