【爆肝细文】1920×1080高清必应壁纸爬取,只为爬虫小白

爬虫仅供学习,其余概不负责,如需转载请私信问我!!!


前言

本文爬虫源码已由 GitHub github.com/2335119327/… 已经收录(内涵更多本博文没有的爬虫,有兴趣的小伙伴可以看看),之后会持续更新,欢迎Star

今天这个爬虫是一个很简单的爬虫,只要稍微有一点基础就能看懂,加油,奥里给,干就完事了!!!

在这里插入图片描述


网页分析


多页爬取URL部分

进入首页一看就知道是精品
在这里插入图片描述

滑到底部,好家伙,162页,(●ˇ∀ˇ●),够我玩的了!

在这里插入图片描述

好了,话不多说,想要爬取,首先要弄懂URL

这是第一页的URL
在这里插入图片描述

第二页
在这里插入图片描述

第三页
在这里插入图片描述

这规律不用我多说把,根据当前页数修改p的值就OK了,但有的小伙伴可能会说:第一次没有p=1啊?

在这里插入图片描述

可以看见我们去手动进行p=1访问第一页也是可以成功访问的

在这里插入图片描述

小伙伴们要记号哦!

在这里插入图片描述


图片下载URL部分

本文对于数据解析使用的是Beautiful Soup,没了解过的小伙伴可见我的这篇好文!

Python爬虫利器之Beautiful Soup入门详解,实战总结!!!

打开控制台

可见,一个图片对应一个class值为item的div
在这里插入图片描述

标题

标题是在class值为item的div标签下的子class值为description的div标签下的h3标签中
在这里插入图片描述

下载URL

在这里插入图片描述

def getUrl(curPage,data,page_path):
	# BeautifulSoup进行解析
    data = BeautifulSoup(data,"html.parser")
    div_list = data.find_all(class_="item")
    for div in div_list:
    	#拼接URL
        img_url = "https://bing.ioliu.cn" + div.find(class_="ctrl download")["href"]
        # 获取标题
        title = div.find(class_="description").find("h3").text
        # 因为保存图片名为标题,所以对标题的特俗字符进行处理
        title = replaceTitle(title)
        downLoadImg(curPage,title,img_url,page_path)
复制代码

图片下载

def downLoadImg(curPage,title,img_url,page_path):
    print("正在爬取第" + str(curPage) + "页:" + title)
    # .content 二进制字节流
    img_res = requests.get(url=img_url,headers=headers).content
    # 保存为jpg图片,也可以为png哦!
    with open(page_path + "/" + title + ".jpg","wb") as f:
        f.write(img_res)
    f.close()
复制代码

完整代码

import requests
from bs4 import BeautifulSoup
import os



headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36 Edg/91.0.864.59"
}

path = "./必应图库"


symbol_list = ["\\", "/", "<", ":", "*", "?", "<", ">", "|","\""]

def replaceTitle(title):
    title = "".join(str(title).split())
    for i in symbol_list:
        if title.find(str(i)) != -1:
            title = title.replace(str(i),"")

    return title

def getUrl(curPage,data,page_path):
    data = BeautifulSoup(data,"html.parser")
    div_list = data.find_all(class_="item")
    for div in div_list:
        img_url = "https://bing.ioliu.cn" + div.find(class_="ctrl download")["href"]
        title = div.find(class_="description").find("h3").text
        title = replaceTitle(title)
        downLoadImg(curPage,title,img_url,page_path)

def downLoadImg(curPage,title,img_url,page_path):
    print("正在爬取第" + str(curPage) + "页:" + title)
    img_res = requests.get(url=img_url,headers=headers).content
    with open(page_path + "/" + title + ".jpg","wb") as f:
        f.write(img_res)
    f.close()





if __name__ == '__main__':
    if not os.path.exists(path):
        os.mkdir(path)

    url = "https://bing.ioliu.cn"
    for i in range(1, 3):
        page_path = path + "/第" + str(i) + "页图库"
        if not os.path.exists(page_path):
            os.mkdir(page_path)
        if i > 1:
            url = url + "/?p=" + str(i)
        response = requests.get(url=url, headers=headers)
        getUrl(i,response.text,page_path)
复制代码


爬取结果(高清大图,看着都是享受)

因为测试,所以只怕爬取了两页
在这里插入图片描述

==都是1920×1080的哦!觉得不错的小伙伴可以给个三连,感谢支持😁==

在这里插入图片描述


最后

我是 Code皮皮虾,一个热爱分享知识的 皮皮虾爱好者,未来的日子里会不断更新出对大家有益的博文,期待大家的关注!!!

创作不易,如果这篇博文对各位有帮助,希望各位小伙伴可以==一键三连哦!==,感谢支持,我们下次再见~~~

分享大纲

大厂面试题专栏

Python爬虫专栏

本文爬虫源码已由 GitHub github.com/2335119327/… 已经收录(内涵更多本博文没有的爬虫,有兴趣的小伙伴可以看看),之后会持续更新,欢迎Star


在这里插入图片描述