小学课后服务 Python少儿编程 进阶篇:5-图片爬取 课件 (21张PPT)

资源下载
  1. 二一教育资源

小学课后服务 Python少儿编程 进阶篇:5-图片爬取 课件 (21张PPT)

资源简介

(共21张PPT)
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
少儿编程课
王者荣耀图片爬虫
本节课,我们来爬取网站上面的图片并保存到电脑中:
图片爬取及保存
生活中保存网络图片的方法:
1
首先找到要保存的图片;
2
鼠标右键保存图片到指定目录:
少量的图片还可以这样操作,那如果是一整页的图片呢?很多页面的图片呢?
1
每个图片的网络地址就像一根根蛛丝,图片就是蛛丝上面的小水珠。
编码实现
编程实现保存网络图片的思路:
1
首先找到要保存的图片,获取图片地址
2
保存图片到本地目录,并重命名为图片原名称
编程实现保存本地图片:
1
编码保存图片到指定目录:
from tkinter import *
from PIL import Image
from PIL import ImageTk
root = Tk()
root.title('标签上图片的展示')
img = Image.open('C:/Users/Administrator/Desktop/momeimei.jpg')
img = ImageTk.PhotoImage(img)
label = Label(root, text="", image=img, width=500, height=500)
label.grid(row=0, column=0)
在之前生成二维码的课程中,我们使用Image和ImageTkinter来操作图片吗?
# 将本地的图片文件保存到其他的目录中
img.save('C:/Users/Administrator/Desktop/momeimei-1.jpg')
编程实现保存一个网络图片:
1
拿到图片网络地址:http://pic1./wallpaper/2018-11-13/5bea31214169a_270_185.jpg
使用Requests获取网络图片
import requests
imageUrl = 'http://pic1./wallpaper/2018-11-09/5be4ef509f62a_270_185.jpg'
imgRes = requests.get(imageUrl)
2
图片属于二进制文件,Requests 会自动为你解码 gzip 和 deflate 传输编码的响应数据。
例如,以请求返回的二进制数据创建一张图片,你可以使用如下代码:
import requests
from io import BytesIO
img = Image.open(BytesIO(imgRes.content))
3
使用Image将图片保存到本地目录
# 将本地的图片文件保存到其他的目录中
img.save('C:/Users/Administrator/Desktop/momeimei-1.jpg')
练习
Exercises


线





现在知道网络图片地址,请使用Image的save()方法
将它保存到本地目录下。
使用BeautifulSoup拿到图片的地址:
1
现在知道了网页的地址,每个图片的地址包含在网页中,如何拿到图片的下载地址?
BeautifulSoup安装
网络数据挖掘指的是从网站中获取数据的过程,数据挖掘技术可以让我们从网站世界中收集大量有价值的数据。
Beautiful Soup是一个Python库,可以从HTML或XML文件中获取数据,利用它你可以做很多事情,
比如你可以持续解析某个商品的最新价格,以便跟踪价格的波动情况。
1
Beautiful Soup简介
2
Beautiful Soup安装
# 使用pip安装
pip install beautifulsoup4
# 检验是否安装成功
from bs4 import BeautifulSoup
3
Beautiful Soup支持Python标准库中的HTML解析器,还支持一些第三方的解析器,其中一个是 lxml
# lxml是功能最丰富且易于使用的库,用于处理Python语言中的XML和HTML
pip install lxml
# html5lib的解析方式与浏览器相同
pip install html5lib
BeautifulSoup使用
4
创建Beautiful Soup对象
from bs4 import BeautifulSoup
html_doc = """
The Dormouse's story

The Dormouse's story


Once upon a time there were three little sisters; and their names were
Elsie,
Lacie and
Tillie;
and they lived at the bottom of a well.


...


"""
soup = BeautifulSoup(html_doc,features='lxml')
print(soup)
使用BeautifulSoup解析这段代码,能够得到一个 BeautifulSoup 的对象,并能按照标准的缩进格式的结构输出
BeautifulSoup使用
5
使用find_all()找到指定标签
soup = BeautifulSoup(html_doc,features='lxml')
print(soup.fidn_all(‘a’))
[
Elsie,
Lacie,
Tillie
]
输出结果如下:
6
使用网页内容创建BeautifulSoup对象
import requests
htmlUrl = 'http://www./zt/wangzherongyao_1.html'
soup = BeautifulSoup(requests.get(htmlUrl).text,features='lxml')
练习
Exercises


线





现在知道网页地址,请使用BeautifulSoup的find_all()
方法找到所有图片地址并打印出来。
图片地址的筛选
7
使用find_all()找到标签
htmlUrl = 'http://www./zt/wangzherongyao_1.html'
soup = BeautifulSoup(requests.get(htmlUrl).text,features='lxml')
for url in soup.find_all('img'):
print(url)
王者荣耀

...

...

...
经典热门游戏桌面壁纸大全

输出结果如下:
图片地址的筛选
8
使用has_attr()过滤标签
# 如果有data-original属性并且没有'alt属性
if (child.has_attr('data-original') and not child.has_attr('alt')):

输出结果如下:
9
获取标签数据
# 获取data-original属性的数据
child.['data-original’]
http://pic1./wallpaper/2018-11-13/5bea31214169a_270_185.jpg
输出结果如下:
完成代码如下:
import requests
from PIL import Image
from io import BytesIO
from bs4 import BeautifulSoup
imgUrlHead = ‘http://www./zt/wangzherongyao_'
imgUrlTail = ‘.html'
res = requests.get(url)
soup = BeautifulSoup(res.text,features='lxml')
def saveImage(url):
names = url.split('/')
imgName = names[len(names)-1]
imgRes = requests.get(url)
img = Image.open(BytesIO(imgRes.content))
img.save(downloadPath+imgName)
for i in range(1,6):
tempUrl = '%s%s%s' % (imgUrlHead,i,imgUrlTail)
for child in soup.find_all('img'):
if (child.has_attr('data-original') and not child.has_attr('alt')):
url = child['data-original']
saveImage(url)
总结
Summary
网络请求获取网络图片,并保存到本地目录

BeautifulSoup的使用

Thanks!

展开更多......

收起↑

资源预览