幽灵资源网 Design By www.bzswh.com
导入相关包
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 = "" + 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()
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
python,知乎,图片爬取
幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com
暂无评论...
《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线
暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。
艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。
《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。