爬虫之下载网页图片"/>
Python小白进阶——网络爬虫之下载网页图片
目录
- 获取网页内容
- 创建存取图片的文件夹
- 从网页内容中找到图片链接
- 取得并保存图片
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小白进阶——网络爬虫之下载网页图片
发布评论