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

python爬取优美图库海量图片,附加代码,一键爬取

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

python爬取优美图库海量图片,附加代码,一键爬取

优美高清图片为大家提供高清美女套图赏析,非高清不录入,大家的网速要给力。

今天教大家爬取优美图库网站中高质量的图片!!

效果如下:

使用工具:

Python 3.9

pycharm

主要内容:

1、系统分析目标网页

2、海量图片数据一键保存文件夹

爬虫的一般思路: 1.拿到主页面的源代码,提取子页面的链接地址,href
2.通过href拿到子页面的内容,从子页面中找到图片的下载地址 img->src
3.下载图片
代码:
#1.拿到主页面的源代码,提取子页面的链接地址,href
#2.通过href拿到子页面的内容,从子页面中找到图片的下载地址 img->src
#3.下载图片
import requests
from bs4 import BeautifulSoup
import time
url = "https://umei.cc/bizhitupian/meinvbizhi/"
url2 = "https://umei.cc/"
resp = requests.get(url)
resp.encoding = 'utf-8'
#将源代码交给bs
#print(resp.text)
page = BeautifulSoup(resp.text,"html.parser")
alist = page.find("div","TypeList").find_all("a")
#print(alist)
for a in alist:
    #print(url2+a.get('href')) #直接通过get就可以拿到属性的值
    #拿到子页面的源代码
    href = url2+a.get('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")
    im = child_page.find("div","ImageBody")
    img = im.find("img")
    #print(img.get("src"))
    src = img.get("src")
    #下载图片
    img_resp = requests.get(src)
    #img_resp.content #这里拿到的是字节
    img_name = src.split("/")[-1] #拿到url中最后一个/后的内容
    with open("img/"+img_name,mode="wb") as f:
        f.write(img_resp.content) #图片内容写入到文件
    print("over!!!",img_name)
    time.sleep(1)
print("all over!!!")


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

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

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