栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

爬取壁纸图片

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

爬取壁纸图片

# 1.拿到主页面源代码
# 2.从中提取出子页面链接地址 a->href
# 3.进入子页面并获取到子页面源代码
# 4.从源代码中找到图片的链接地址 img->src
# 5.下载(获取字节文件)

import requests
from bs4 import BeautifulSoup
import time

url = 'https://www.umei.cc/bizhitupian/weimeibizhi/'
resp = requests.get(url)
resp.encoding = 'utf-8'  # 解决中文乱码问题
# print(resp.text)
# 将源码交给bs4
main_page = BeautifulSoup(resp.text, 'html.parser')
# print(main_page)
aList = main_page.find('div', class_='TypeList').find_all('a')
# print(aList)
for b in aList:
    child_page_url = b.get('href')
    child_page = requests.get('https://www.umei.cc' + child_page_url)  # 获取子页面的源码
    child_page.encoding = 'utf-8'
    child_page_text = child_page.text  # 获取页面源文件
    child_page_bf = BeautifulSoup(child_page_text, 'html.parser')
    img = child_page_bf.find('p', align='center').find('img')  # 获取img元素
    src = img.get('src')  # 获取图片的src地址
    # 下载图片
    img_resp = requests.get(src)
    img_name = src.split('/')[-1]
    with open('img/'+img_name, mode='wb') as f:
        f.write(img_resp.content)
        print('下载完成')
    time.sleep(1)  # 时间休眠

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/283468.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号