Python小白进阶——网络爬虫之下载网页图片

编程入门 行业动态 更新时间:2024-10-26 05:17:33

Python小白进阶——网络<a href=https://www.elefans.com/category/jswz/34/1770264.html style=爬虫之下载网页图片"/>

Python小白进阶——网络爬虫之下载网页图片

目录

  1. 获取网页内容
  2. 创建存取图片的文件夹
  3. 从网页内容中找到图片链接
  4. 取得并保存图片

1.获取网页内容

这里以下载网易LOFTER中的照片为例。这里选择的链接是“/”

import os,requests
from bs4 import BeautifulSoup
from urllib.request import urlopen url = '/'
html = requests.get(url)
html.encoding = 'utf-8'
sp = BeautifulSoup(html.text,"html.parser")

使用requests.get获取网页内容,用encoding设置符合网页编码类型。使用爬虫利器BeautifulSoup和html.parser解析网页。

2.创建存取图片的文件夹

#创建images目录保存图片
images_dir = "images/"
if not os.path.exists(images_dir):os.mkdir(images_dir)

3. 从网页内容中找到图片链接

更多推荐

Python小白进阶——网络爬虫之下载网页图片

本文发布于:2024-02-12 23:32:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1689839.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:爬虫   下载网页   图片   网络   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!