【爬虫】下载图片

# -*- coding:utf-8 -*-
# 1、拿取出页面的源代码,然后提取到子页面的链接地址,href
# 2、通过href拿到子页面内容,从子页面找到图片下载地址 img->src
# 3、下载图片

import requests
from bs4 import BeautifulSoup
import time

url = "https://www.umeitu.com/bizhitupian/weimeibizhi/"
resp = requests.get(url)
resp.encoding = "utf-8"  # 处理乱码
# print(resp.text)
# 把源代码交给bs4
main_page = BeautifulSoup(resp.text, "html.parser")
# print(main_page)
# div = main_page.find("div",class_="swiper-wrapper after")
div = main_page.find("div", attrs={"class": "swiper-wrapper after"})
alist = div.find_all("a")
# print(alist)

for a in alist:
    href = "https://www.umeitu.com" + a.get("href")
    # print(href)
    # 拿到子页面源代码
    child_page_resp = requests.get(href)
    child_page_resp.encoding = "utf-8"
    child_page_text = child_page_resp.text
    # 从子页面中拿到下载路径
    child_page = BeautifulSoup(child_page_text, "html.parser")
    div = child_page.find("div", attrs={"class": "content-box"})
    image = div.find("img")
    src = image.get("src")  # 从子页面找到图片下载地址 img->src,直接get属性即可

    img_resp = requests.get(src)
    # img_resp.content 这里拿到的是字节
    image_name = src.split("/")[-1]  # 拿到url中的最后一个/以后的内容

    # 邮件文件夹->Mark directory as->Excluded
    with open("/python/download/image/" + image_name, mode="wb") as f:
        f.write(img_resp.content)  # 图片内容写入文件

    print("over", image_name)
    time.sleep(1)

    f.close()

print("all over")

 

posted @   hanyr  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
点击右上角即可分享
微信分享提示