小猴子的女朋友最近要求老多了,前几天发现一个宝贝网站,上面的图片真的很不错,就想把图片都保存下载。

可是她右键另存为没反应,就过来找我。

女朋友:这个网站的图片怎么都不能右键保存呢?

小猴子:怎么可能,只要是眼睛能看到的,都可以保存。

女朋友:那你帮我把这个网站上的图片都下载下来吧,都要分好类,每个文件夹一类。

小猴子:……

查看图片的源地址

右键保存,其实是前端页面禁用了鼠标的右键另存的行为而已,想找到图片的源地址很简单。在我之前的文章我也介绍过,还是F12打开开发者调试工具,定位到图片,其中src就是图片的源地址。


批量下载图片

但是分类吧,图片那么多,类别又很多,手动操作起来真的很麻烦。还是用代码来解决吧。

我们会用到两个很有用的python库,beautifulsoup和you-get,后期再单独介绍这两个库,先把女朋友的任务完成了

分析目标网站

https://7esl.com/picture-dictionary/

每个类别都有一个目录,点击就可跳转到目标页面,目标页面上有大量该类别的图片。


步骤一:获取网页html源码

def get_html(url):    res = requests.get(url)return res.text

步骤二:获取下载目录及跳转链接

def get_sub_url(base_url):    html = get_html(base_url)    soup = BeautifulSoup(html, "lxml")    soup_ul = soup.find_all("ul")    pic_url_dict = {}for ul_item in soup_ul:if ul_item['class'][0] == "bellows-nav":for a_item in ul_item.find_all("a"):                item_content = a_item.get_text()                item_link = a_item.get("href")                pic_url_dict[item_content] = item_linkreturn pic_url_dict

知识点:beautifulsoup处理html,提取目录和跳转链接

步骤三:获取图片源地址

def get_pic_url(pic_url):    html = get_html(pic_url)    soup = BeautifulSoup(html, "lxml")    pic_url_lst = []for item in soup.find_all("img"):        item_src = item.get("src")if item_src.startswith("https") and item_src[-4:] in (".jpg", ".png", ".bmp"):            pic_url_lst.append(item_src)return pic_url_lst

知识点:beautifulsoup处理目标网页html,提取网页上所有图片的地址

步骤四:下载目标网页所有图片到指定目录

def dld_pic_to_dir(file_path, pic_url):    you_get_cmd = 'you-get -o "{}" "{}"'.format(file_path, pic_url)    print(you_get_cmd)    os.system(you_get_cmd)

知识点: you-get下载图片到指定目录

完整代码

50行代码搞定!!!

import osimport requestsfrom bs4 import BeautifulSoup

def get_html(url):    res = requests.get(url)return res.text

def get_sub_url(base_url):    html = get_html(base_url)    soup = BeautifulSoup(html, "lxml")    soup_ul = soup.find_all("ul")    pic_url_dict = {}for ul_item in soup_ul:if ul_item['class'][0] == "bellows-nav":for a_item in ul_item.find_all("a"):                item_content = a_item.get_text()                item_link = a_item.get("href")                pic_url_dict[item_content] = item_linkreturn pic_url_dict

def get_pic_url(pic_url):    html = get_html(pic_url)    soup = BeautifulSoup(html, "lxml")    pic_url_lst = []for item in soup.find_all("img"):        item_src = item.get("src")if item_src.startswith("https") and item_src[-4:] in (".jpg", ".png", ".bmp"):            pic_url_lst.append(item_src)return pic_url_lst

def dld_pic_to_dir(file_path, pic_url):    you_get_cmd = 'you-get -o "{}" "{}"'.format(file_path, pic_url)    print(you_get_cmd)    os.system(you_get_cmd)

if __name__ == '__main__':    picture_dictionary_base_url = "https://7esl.com/picture-dictionary/"    pic_url_dict = get_sub_url(picture_dictionary_base_url)    save_path = "d:/haha"for dir_name, sub_url in pic_url_dict.items():        print(dir_name, sub_url)        output_pic_path = os.path.join(save_path, dir_name)if not os.path.exists(output_pic_path):            os.makedirs(output_pic_path)for pic_src in get_pic_url(sub_url):            pic_name = pic_src.split("/")[-1]if os.path.exists(os.path.join(output_pic_path, pic_name)):                print(pic_name, " exist")else:                print(pic_src)                dld_pic_to_dir(output_pic_path, pic_src)

效果



猴哥带你飞,青春不迷茫~~

<<>>

点个“在看”,好东西跟朋友一起分享

cmd 看图片十六进制_Fun Python | 女朋友让我把这网站上的图片都下载下来相关推荐

  1. python爬取汽车之家_python爬虫实战之爬取汽车之家网站上的图片

    随着生活水平的提高和快节奏生活的发展.汽车开始慢慢成为人们的必需品,浏览各种汽车网站便成为购买合适.喜欢车辆的前提.例如汽车之家网站中就有最新的报价和图片以及汽车的相关内容,是提供信息最快最全的中国汽 ...

  2. 网站图片多服务器选多大,网站上的图片一般多大合适

    网站上的图片一般多大合适 内容精选 换一换 安装了SSL证书后,访问网站时,HTTPS比HTTP要多几次握手的时间,HTTPS协议握手阶段比较费时,同时还要进行RSA校验,因此使用了SSL证书后,相较 ...

  3. php获取远程网页地址吗,php怎么获取远程网站上的图片的地址?有什么思路吗?...

    php怎么获取远程网站上的图片的地址?有什么思路吗? 比如获取百度的背景图片的路径 https://ss0.baidu.com/7Po3dSag_xI4khGko9WTAnF6hhy/super/cr ...

  4. Python爬虫练习:爬取网站动漫图片

    前言 有一段没用 python 了,我也不知道自己为什么对 python 越来越淡,可能自己还是比较喜欢 android ,毕竟自己第一次接触编程就是 android,为了android学java,然 ...

  5. 对Python爬虫编写者充满诱惑的网站,《可爱图片》,瞧人这网站名字起的

    这网站名字绝了,当擦哥看到的那一瞬间,我估计他就准备好爬虫代码了. 可爱图片双线程爬取 这篇博客要对 Python 爬虫进行提速了,实现双线程爬虫.而且在爬取过程中,还有意外收货. 爬取目标分析 爬取 ...

  6. python爬取某网站上的图片

    1.请求网页 import requests# 请求头,对python爬虫进行伪装 # user-agent:浏览器的身份标识 headers = {'user-agent': 'Mozilla/5. ...

  7. python 爬网站上的图片

    最近在做bot的动物识别,最后根据大会给出来的测试数据,发现对简笔画的动物识别处于懵圈状态,识别效果很差~故我需要自己爬取一些简笔画的图片~ 手写学习了某一网站的图片爬取: 附上代码: # -*- c ...

  8. 如何使用VB批量采集指定网站上的图片文件以及网页内文字等资源素材

    做自媒体的,可能需要到采集网络上的图片及文章等素材,手动一张张去右键下载效率当然太低 了.还有的朋友不喜欢动脑筋,喜欢到网上搜索一些工作总结啊,或是看小说啊那些文字看得到复制不了,要是能有个小工具来帮 ...

  9. 我使用Python和Django在自己的网站上建立了一个会员专区。 这是我学到的东西。

    I decided it was time to upgrade my personal website in order to allow visitors to buy and access my ...

  10. java随机抓取网站上的图片_抓取一个网站特定的全部图片(JAVA)

    1. 目的 用五笔时,如果碰到不会拆的字,只好换回拼音.但这样做治标不治本,于是到网上找五笔反查工具.最后发现一个不错的网站--不仅有每个字对应的五笔码,还有其字根图.可惜的是,这是一个网站.换句说, ...

最新文章

  1. AtCoder Beginner Contest 202 D - aab aba baa(组合计数,字典序)
  2. How I Hacked 40 Websites in 7 minutes
  3. HLS Pargmas(2) interface
  4. 大话设计模式—原型模式
  5. 两数的最大公约数算法基础及优化
  6. hadoop+hive-0.10.0完全分布式安装方法
  7. server sql 水平分表_springboot集成Shardingsphere进行分库分表
  8. 百度又要开放哪些无人车新能力?“老司机”Apollo3.5要来了,市中心开车无压力...
  9. requirejs 学习笔记 0
  10. 51Nod-1486 大大走格子
  11. 201903-2 二十四点
  12. 【山大智云】SeafileServer源码分析之CDC(基于内容长度可变分块)
  13. 二年级课程表(4月18日-4月22日)
  14. 神话人物马化腾的“神话”
  15. 小辩五笔输入法的高效及拼音输入法的盲目夸大--评所谓整句输入技术
  16. ubuntu无法识别android手机
  17. [BJDCTF 2nd]燕言燕语-y1ng解析
  18. Vue3动态引入Element-plus icon图标
  19. 堆排序中非叶子节点的位置怎么算
  20. js面向对象编程基础

热门文章

  1. 更加全面的ASP.NET AJAX(Atlas)学习、参考资源(英文)
  2. 微信公众号(静默授权和分享)
  3. Win8.1下COCOS2D-X 3.4环境搭建
  4. 微信客户端抽奖转盘效果
  5. 蜘蛛侠天堂,打死我mac键盘
  6. R中读取Excel大文件
  7. 路由模块router实现step1
  8. Android使用弹出式对话框
  9. Python使用matplotlib可视化模拟闯红灯现象柱状图
  10. concurrenthashmap获取不到_面试必问的ConcurrentHashMap