今天图有兴趣的去爬取了百度图片图片,想选择一定的尺寸、高清度等条件信息获取指定的图片,经过分析:

以下搜索:迪丽热巴,选择高清、特大尺寸【1600*2860像素】图片获取。

如果:直接获取则获取下来的尺寸是不对的,大小仅为:500 x 444像素。

经过百度图片的官网分析,此网站数据是采用Ajax技术动态加载图片数据,即:当鼠标向下滚动时,会再次请求数据库,数据库通过json的数据传输格式发送到前端,通过浏览器加载渲染。因此如果要获取多张图片时,需要构造多页的URL。

做为程序员,Google公司的Chrome浏览器是开发的利器,其能实现基本的‘抓包’功能【即检测到浏览器发送给服务器的请求,同时也能看到服务器响应给浏览器的请求】。Ajax技术实现的数据传输,可以通过以下方式观测到:

选择其中的请求,观察规律:

https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=9&ic=0&hd=1&latest=0&copyright=0&word=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&s=&se=&tab=&width=0&height=0&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=30&rn=30&gsm=1e&1565249894421=https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=9&ic=0&hd=1&latest=0&copyright=0&word=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&s=&se=&tab=&width=0&height=0&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=60&rn=30&gsm=3c&1565249897307=https://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&cl=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=9&ic=0&hd=1&latest=0&copyright=0&word=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&s=&se=&tab=&width=0&height=0&face=0&istype=2&qc=&nc=1&fr=&expermode=&force=&pn=90&rn=30&gsm=5a&1565249897413=

通过对比观察:

queryWord:表示要查询的关键字,如果是中文,则是通过URL编码。

word:也表示要查询的关键字,如果是中文,也是通过URL编码。

hd:值为1,表示高清

pn:表示第几页,如:第2页是: (2-1)*30, 第三页:(3-1)*30

rn:表示每页显示的图片数量。

则选择其中的一个URL,查看其数据,由于是采用Ajax技术实现,其数据格式为 Json格式,则要较为直观的观察其数据,可以通过Json视图查看,可以打开其网址:https://www.json.cn/, 粘贴json格式数据,则其会较为直观的展示其图片内容:

以上有图片的地址:

分别选择以上:thumbURL、middleURL、hoverURL对应的URL地址下载,查看对应的图片信息:

thumbURL

middleURL

hoverURL

500 x 890像素

500 x 890像素

500 x 890像素

以上三种方式所对应的RUL均不是 特大尺寸【1600*2860像素】的图片的URL地址,通过点击进入详细页后下载出来的图片尺寸大小则是正确的,则分析详细页的URL地址:

https://image.baidu.com/search/detail?ct=503316480&z=9&ipn=d&word=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&step_word=&hs=0&pn=1&spn=0&di=171930&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=1678640081%2C3765101474&os=12190547%2C426740624&simid=3367237481%2C175217088&adpicid=0&lpn=0&ln=652&fr=&fmq=1565249787800_R&fm=result&ic=0&s=undefined&hd=1&latest=0&copyright=0&se=&sme=&tab=0&width=0&height=0&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201811%2F10%2F20181110124915_NuNw4.jpeg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B17tpwg2_z%26e3Bv54AzdH3Fks52AzdH3F%3Ft1%3D8a8m0mlcnm&gsm=1e&rpstart=0&rpnum=0&islist=&querylist=&force=undefinedhttps://image.baidu.com/search/detail?ct=503316480&z=9&ipn=d&word=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&step_word=&hs=0&pn=2&spn=0&di=56760&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=1392268776%2C1636379493&os=8832284%2C1407244711&simid=3411309683%2C394722903&adpicid=0&lpn=0&ln=652&fr=&fmq=1565249787800_R&fm=result&ic=0&s=undefined&hd=1&latest=0&copyright=0&se=&sme=&tab=0&width=0&height=0&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fi3.bbs.fd.zol-img.com.cn%2Fg5%2FM00%2F00%2F00%2FChMkJ1mdq7yIPiWoAAbvPaQyHU0AAf7HADEgHkABu9V949.jpg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fkkf_z%26e3Bz5s_z%26e3Bv54_z%26e3BvgAzdH3Ff3kkfAzdH3F180lc_m80aa_7t1_ywg23tg28ada_z%26e3Bip4s&gsm=1e&rpstart=0&rpnum=0&islist=&querylist=&force=undefinedhttps://image.baidu.com/search/detail?ct=503316480&z=9&ipn=d&word=%E8%BF%AA%E4%B8%BD%E7%83%AD%E5%B7%B4&step_word=&hs=0&pn=4&spn=0&di=90860&pi=0&rn=1&tn=baiduimagedetail&is=0%2C0&istype=2&ie=utf-8&oe=utf-8&in=&cl=2&lm=-1&st=-1&cs=1221790608%2C2201828612&os=4260874520%2C241316579&simid=3392209277%2C577512865&adpicid=0&lpn=0&ln=652&fr=&fmq=1565249787800_R&fm=result&ic=0&s=undefined&hd=1&latest=0&copyright=0&se=&sme=&tab=0&width=0&height=0&face=undefined&ist=&jit=&cg=&bdtype=0&oriquery=&objurl=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201810%2F16%2F20181016031010_jrA8R.jpeg&fromurl=ippr_z2C%24qAzdH3FAzdH3Fooo_z%26e3B17tpwg2_z%26e3Bv54AzdH3Fks52AzdH3F%3Ft1%3D8a8n9b8lc0&gsm=1e&rpstart=0&rpnum=0&islist=&querylist=&force=undefined

通过对几张图片的URL地址观察:

objURL:表示图片URL地址的来源,后跟的URL地址就是图片的正确的URL。

ippr_z2C$qAzdH3FAzdH3Fk-ffs_z&e3B17tpwg2_z&e3Bv54AzdH3F7rs5w1fAzdH3Ftpj4AzdH3Fda8bacAzdH3Fa9AzdH3Fda8baca9ddcd88_pSVbt_z&e3B3rj2ippr_z2C$qAzdH3FAzdH3Fk-ffs_z&e3B17tpwg2_z&e3Bv54AzdH3F7rs5w1fAzdH3Ftpj4AzdH3Fda8b88AzdH3F8aAzdH3Fda8b888a8d9l8c_N7No9_z&e3B3rj2ippr_z2C$qAzdH3FAzdH3Ftn_z&e3Bkkf_z&e3Bu1_z&e3Bz5s-t42_z&e3Bv54_z&e3BvgAzdH3F2cAzdH3FMaaAzdH3FaaAzdH3FaaAzdH3FCiMhJ841q0yIPtW5AAkePwQyHUaAAu0HADE2HhAB7lVl9l_z&e3B3r2

很明显这不是一个正确的URL地址,这是百度图片对此数据进行了加密,因此,需要解密出正确的URL地址。

1、首先展示图片的编码:

在响应中 ‘cores_ddf2bfa.js’ 中就存在有响应的编码对应的数据。

base_encode = {'_z2C$q': ':','_z&e3B': '.','AzdH3F': '/','w': 'a','k': 'b','v': 'c','1': 'd','j': 'e','u': 'f','2': 'g','i': 'h','t': 'i','3': 'j','h': 'k','s': 'l','4': 'm','g': 'n','5': 'o','r': 'p','q': 'q','6': 'r','f': 's','p': 't','7': 'u','e': 'v','o': 'w','8': '1','d': '2','n': '3','9': '4','c': '5','m': '6','0': '7','b': '8','l': '9','a': '0','-': '-'
}

2、对获取的图片地址进行解码

def parse_strins(objurl):if '_z2C$q' in base_encode:objurl = objurl.replace('_z2C$q', ':')if '_z&e3B' in obbase_encodejurl:objurl = objurl.replace('_z&e3B', '.')if 'AzdH3F' in base_encode:objurl = objurl.replace('AzdH3F', '/')res = ''for s in objurl:if s in base_encode:res += sign_table[s]else:res += sreturn res

以下选择了几个实例:

fromurls = ['ippr_z2C$qAzdH3FAzdH3Fk-ffs_z&e3B17tpwg2_z&e3Bv54AzdH3F7rs5w1fAzdH3Ftpj4AzdH3Fda8bacAzdH3Fa9AzdH3Fda8baca9ddcd88_pSVbt_z&e3B3rj2','ippr_z2C$qAzdH3FAzdH3Fk-ffs_z&e3B17tpwg2_z&e3Bv54AzdH3F7rs5w1fAzdH3Ftpj4AzdH3Fda8b88AzdH3F8aAzdH3Fda8b888a8d9l8c_N7No9_z&e3B3rj2','ippr_z2C$qAzdH3FAzdH3Ftn_z&e3Bkkf_z&e3Bu1_z&e3Bz5s-t42_z&e3Bv54_z&e3BvgAzdH3F2cAzdH3FMaaAzdH3FaaAzdH3FaaAzdH3FCiMhJ841q0yIPtW5AAkePwQyHUaAAu0HADE2HhAB7lVl9l_z&e3B3r2'
]for u in fromurls:resUrl= parse_strins(u)print(resUrl)'''
http://b-ssl.duitang.com/uploads/item/201805/04/20180504225211_tSV8i.jpeg
http://b-ssl.duitang.com/uploads/item/201811/10/20181110124915_NuNw4.jpeg
http://i3.bbs.fd.zol-img.com.cn/g5/M00/00/00/ChMkJ1mdq7yIPiWoAAbvPaQyHU0AAf7HADEgHkABu9V949.jpg
'''

结果正确的解析出了图片的地址,同时对应的尺寸也是选择的特大尺寸。

完整代码:

import requests
import json
from urllib.parse import quote
import urllib.request
import os# 百度图片加密规则
base_encode = {'_z2C$q': ':','_z&e3B': '.','AzdH3F': '/','w': 'a','k': 'b','v': 'c','1': 'd','j': 'e','u': 'f','2': 'g','i': 'h','t': 'i','3': 'j','h': 'k','s': 'l','4': 'm','g': 'n','5': 'o','r': 'p','q': 'q','6': 'r','f': 's','p': 't','7': 'u','e': 'v','o': 'w','8': '1','d': '2','n': '3','9': '4','c': '5','m': '6','0': '7','b': '8','l': '9','a': '0','-': '-'
}
objURL = []         # 用于存储json格式的objURL地址
url_list = []       # 用于存储解码后的图片URL地址# 伪装身份与解决防盗链
header = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36','Referer': 'https://image.baidu.com/'}# 通过url地址返回网页源码
def get_html(url):rep = requests.get(url, headers=header)if rep.status_code == 200:return rep.textelse:return None# 获取json数据中objURL数据并存至列表中
def get_objURL(html):if '\\' in html:html = html.replace('\\', '')print(html)datas = json.loads(html)ress = datas['data'][:-1]for res in ress:objURL.append(res["objURL"])# 解码objURL地址,并存至解析后的列表中
def parse_strins(objurl):if '_z2C$q' in objurl:objurl = objurl.replace('_z2C$q', ':')if '_z&e3B' in objurl:objurl = objurl.replace('_z&e3B', '.')if 'AzdH3F' in objurl:objurl = objurl.replace('AzdH3F', '/')res = ''for s in objurl:if s in base_encode:res += base_encode[s]else:res += surl_list.append(res)# 创建并返回图片的存储路径
def create_path(names):path = os.path.join(os.getcwd(), '{}'.format(names))if not os.path.exists(path):os.mkdir(path)return path# 实现图片文件的下载
def download_image(path, image_url, i):end_type = os.path.splitext(image_url)[-1]imageName = os.path.join(path, f'{i}{end_type}')try:urllib.request.urlretrieve(image_url, imageName)print('已下载第{}张图片...'.format(i + 1))except:pass# 实现以上函数的调用与功能的实现
def main():while True:names = input('请输入要查询的关键字:')if not names:breakpath = create_path(names)try:pages = int(input('请输入爬取的页码数:'))except:print('请输入正确的页码数【int】')print('-' * 100)else:for i in range(pages):print('正在爬取第 {} 页的数据...'.format(i + 1))page = i * 30urls = 'http://image.baidu.com/search/acjson?tn=resultjson_com&ipn=rj&ct=201326592&is=&fp=result&queryWord={0}=2&lm=-1&ie=utf-8&oe=utf-8&adpicid=&st=-1&z=&ic=&hd=&latest=&copyright=&word={0}&s=&se=&tab=&width=1920&height=1080&face=0&istype=2&qc=&nc=&fr=&expermode=&force=&cg=star&pn={1}&rn=30&gsm=1f'.format(quote(names), page)print(urls)html = get_html(urls)if html:get_objURL(html)for obj in objURL:parse_strins(obj)for m in range(len(url_list)):download_image(path, url_list[m], m)if __name__ == '__main__':main()

百度图片爬虫【图片编码处理】相关推荐

  1. python爬虫图片工具安卓版下载_python图片爬虫(图片爬虫工具) 1.1绿色版

    python图片爬虫(图片爬虫工具)是一款专门为互联.it行业的小伙伴们打造的爬虫工具,可以帮大家对软件进行优化.seo的人一定会用的上哦,有兴趣的赶紧下载了哦!python图片爬虫代码如下: #-* ...

  2. python爬虫——百度贴吧图片爬取 小项目

    项目目的: 爬取贴吧中所有帖子里面的图片 将爬取到的图片存储到名称为贴吧名称的文件夹中 项目环境 python版本:python3.6 用到的库:requests.etree.unquote 浏览器: ...

  3. Python爬虫,爬取百度贴吧图片和视频文件,xpath+lxml,访问被拒的原因分析

    目录 百度贴吧图片和视频文件爬取程序 1.需求分析 2.url分析 3.Xpath分析 4.程序设计 5.坑点 百度贴吧图片和视频文件爬取程序 1.需求分析 进入百度贴吧,搜索周杰伦,进入周杰伦吧.我 ...

  4. 【python--爬虫】百度图片爬虫

    如何快速收集某个关键字的图片呢?有的小伙伴可能会说百度图片,bingo答对了!o( ̄▽ ̄)o,博主本次就讲解下如何爬取百度图片 环境准备 为了优雅的享用这盘python爬虫大餐,请各位读者大大准备要以 ...

  5. python:从零开始的百度图片爬虫

    python百度图片爬虫 最近打算尝试接触人工智障,于是发现需要先搞到数据集,制作数据集又需要带量数据 我打算先搞个图片判断的,类似于看有没有猫这种 所以我需要数据集,需要图片,需要很多很多图片 所以 ...

  6. python爬取百度贴吧xpath_爬虫基础系列xpath实战——爬取百度贴吧图片(3)

    8586231_192932724000_2.jpg #百度贴吧图片爬虫 # 爬取步骤:1.基础设置,2.构造url,3.爬取页面,4.解析页面,5.保存页面 # 采取urllib爬取 #引入必要的包 ...

  7. python 爬取菜鸟教程python100题,百度贴吧图片反爬虫下载,批量下载

    每天一点点,记录学习 python 爬取菜鸟教程python100题 近期爬虫项目,看完请点赞哦: 1:python 爬取菜鸟教程python100题,百度贴吧图片反爬虫下载,批量下载 2:pytho ...

  8. 最简洁的百度图片爬虫

    今天写了一个 百度图片爬虫,代码如下 import requests import re import os import timeheaders = {'User-Agent':'Mozilla/5 ...

  9. python爬虫:百度图片爬虫代码

    代码里的logid我也不确定有没有时效,如果有的话请大家自行替换,就在数据包标头那里,如图 代码来了,来了,来了....... 详细解说在这里:百度图片爬虫代码详解 #Author:Griffy #D ...

  10. python 爬虫案例:爬取百度贴吧图片

    文章更新于:2020-04-24 注1:打包后的程序(无需python环境)下载参见:https://ww.lanzous.com/ibvwref 注2:更多爬虫案例参见:https://github ...

最新文章

  1. SimpleDateFormat-时间格式化中的大小写字符
  2. 201112阶段二qt图形视图框架
  3. 图灵2008年12月出版的计算机图书
  4. spring 数组中随机取几个_准备几个月,面试阿里耗时两周,最终凭借这些知识拿下阿里offer...
  5. Linux查看所有用户及用户管理命令
  6. endnotex9下载安装_endnote x9怎么和word关联?Word中用EndNote X9教程
  7. HBase权威指南【中文版】
  8. 即时获取最新全国省市区县地图json数据(亲测可用)以及echarts中使用中国地图
  9. SFM(Structure from Motion)一点总结
  10. Head First 系列书籍分享,Head First Python (中文版·第2版),Head First Java(中文版):第二版——涵盖Java 5.0...
  11. wsyy应用程序中的服务器错误,wsYY的博客
  12. Java String的intern方法
  13. 冰蝎软件的配置与应用
  14. 最近两周出去面试遇到的面试题(前端初级、长更)
  15. apache安装及配置
  16. Python中使用遍历在列表中添加字典的坑
  17. 【微机原理与概述】微计算机概述
  18. 高效的java异常(Effective Java Exceptions)
  19. emplace_back()
  20. linux搭建音视频服务器,Linux平台部署音视频SDK实现即时通讯功能

热门文章

  1. HTML - 调用腾讯 QQ 进行客服在线聊天(PC)
  2. 【FinE】资本市场理论(1) CAPM模型
  3. 设计模式之简单工厂、工厂方法、抽象工厂
  4. Lab: User role controlled by request parameter:由请求参数控制的用户角色
  5. Cobalt Strike beacon详解
  6. 已经安装了VRay但3dmax的材质编辑器里没有VRay材质的解决过程
  7. 移动端车牌识别与PC端车牌识别有啥区别解析
  8. 凛冬之翼---php写入数据库时汉字全部变为空白
  9. windows机器硬盘不能超过2T详解及图文破解方法
  10. scala中match使用的坑Scala.matchError:(of class java.lang.String)