打卡学习Python爬虫第四天|bs4爬取优美图库的小清新图片

bs4解析比较简单,通过HTML的标签和属性去提取值,find(标签,属性="值")

但是需要了解HTML的语法知识,然后再使用bs4去提取,逻辑和编写难度就会比较简单和清晰。

bs4如何使用?如有如下HTML代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Simple HTML Table</title>
</head>
<body>

<table border="1">
  <tr>
    <td>Row 1, Cell 1</td>
    <td>Row 1, Cell 2</td>
  </tr>
  <tr>
    <td>Row 2, Cell 1</td>
    <td>Row 2, Cell 2</td>
  </tr>
  <tr>
    <td>Row 3, Cell 1</td>
    <td>Row 3, Cell 2</td>
  </tr>
</table>

</body>
</html>

bs4利用标签和属性去提取值,在什么代码中table是表格标签,tr是行,td是列。也就是这个HTML表格包含三个行(tr)和六个单元格(td)。

目标:爬取优美图库的小清新图片

思路:通过小清新图片的源代码获取子页面的链接,再将子页面的链接作为一个url,通过循环访问子页面来获取每一个子页面中的图片。

一、安装bs4(PyCharm终端输入)

bash 复制代码
pip install BeautifulSoup4

# 用清华源
pip install beautifulsoup4 -i https://pypi.tuna.tsinghua.edu.cn/simple

二、 找到网页url

三、查看页面源代码

四、获取全部子链接

python 复制代码
import requests
from bs4 import BeautifulSoup

url = 'https://www.umeituku.com/weimeitupian/xiaoqingxintupian/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
}
resp = requests.get(url, headers=headers)
resp.encoding = 'utf-8'
# print(response.text)
page = BeautifulSoup(resp.text, 'html.parser')
div = page.find("div",class_="TypeList")

alist = div.find_all("a")
for i in alist:
    href = i.get("href")  # 通过get直接拿到属性值,即子页面链接
    print(href)

五、将子页面链接作为新的url访问

六、根据子页面源代码特征提取想要的内容

成功获取图片下载地址:

七、下载并保存图片

完整代码:

python 复制代码
import requests
from bs4 import BeautifulSoup

url = 'https://www.umeituku.com/weimeitupian/xiaoqingxintupian/'
headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36'
}
resp = requests.get(url, headers=headers)
resp.encoding = 'utf-8'
# print(response.text)
page = BeautifulSoup(resp.text, 'html.parser')
div = page.find("div",class_="TypeList")

alist = div.find_all("a")
for i in alist:
    href = i.get("href")  # 通过get直接拿到属性值,即子页面链接

    z_resp = requests.get(href)  # href就是子页面的url
    z_resp.encoding = 'utf-8'
    z_page = BeautifulSoup(z_resp.text, 'html.parser')

    # p = z_page.find("p",align="center")
    # img = p.find("img")
    # src = img.get("src")
    src = z_page.find("p",align="center").find("img").get("src")
    # 下载图片
    img_resp = requests.get(src)
    img_resp.content  # 图片二进制数据

    with open("./img/"+src.split("/")[-1], "wb") as f:
        f.write(img_resp.content)
        print("下载成功")
    f.close()
    z_resp.close()
resp.close()
相关推荐
NPUQS30 分钟前
【Unity 3D学习】Unity 与 Python 互通入门:点击按钮调用 Python(超简单示例)
学习·3d·unity
子夜四时歌31 分钟前
Python详细安装与环境搭建
开发语言·python
Jinkxs32 分钟前
SkyWalking - Python 应用追踪:基于 skywalking-python 的埋点
开发语言·python·skywalking
大头博士先生32 分钟前
【3月考】二级Python最新真题及满分代码合集(基本操作题部分)
开发语言·python
白狐_79833 分钟前
【实战架构】一人抵一家设计公司:基于 ComfyUI + Python RPA + Photoshop 的全自动化工业制图工作流
python·photoshop·rpa
shengli72233 分钟前
Python在金融科技(FinTech)中的应用
jvm·数据库·python
xcLeigh34 分钟前
IoTDB Python原生接口全攻略:从基础读写到高级实战
开发语言·数据库·python·api·iotdb·原生接口·读写数据
User_芊芊君子34 分钟前
文科生封神!Python+AI 零门槛变现:3 天造 App,指令即收入(附脉脉 AI 沙龙干货)
开发语言·人工智能·python
MeowNeko34 分钟前
为什么说程序员重命名时电脑不要带中文?记一次python manage.py runserver时UnicodeDecodeError的原因与解决方案
人工智能·python·chatgpt·中间件·django·utf8
是Dream呀34 分钟前
2025年中秋月亮只有94.91%圆?Python告诉你真相
开发语言·python·中秋节