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

2022/2/4 11:43:20

本文主要是介绍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!!!")




这篇关于python爬取优美图库海量图片,附加代码,一键爬取的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程