侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

python实现知乎高颜值图片爬取

Python  /  管理员 发布于 7年前   177

导入相关包

import timeimport pydashimport base64import requestsfrom lxml import etreefrom aip import AipFacefrom pathlib import Path

百度云 人脸检测 申请信息

#唯一必须填的信息就这三行APP_ID = "xxxxxxxx"API_KEY = "xxxxxxxxxxxxxxxx"SECRET_KEY = "xxxxxxxxxxxxxxxx"# 过滤颜值阈值,存储空间大的请随意BEAUTY_THRESHOLD = 55AUTHORIZATION = "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# 某话题下讨论列表请求 urlBASE_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()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


  • 上一条:
    Python实现网页截图(PyQT5)过程解析
    下一条:
    python3 enum模块的应用实例详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 在go语言中实现IP/CIDR的ip和netmask互转及IP段形式互转及ip是否存在IP/CIDR(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客