爬虫使用bs4爬取两个壁纸网站

该博客展示了如何使用Python的requests和BeautifulSoup库抓取两个网站上的高清壁纸。作者创建了fun1和fun4函数分别用于爬取两个不同网址的图片,并通过for循环遍历指定页码范围,将下载的图片保存到本地文件夹中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

可以更改对应得值,来爬取对应得页码以及图片个数,新手上路,如有不足欢迎指导。

import requests
from bs4 import BeautifulSoup
import os
import time

url = 'https://www.4kbizhi.com/index_%d.html'
url1 = 'https://pic.netbian.com/index_%d.html'
headers = {'User-Agent':
               'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0'
           }

if not os.path.exists('./picture1'):
    os.mkdir('./picture1')


# 抓取一到30个  

def fun1(soup):
    for number in range(1, 5):
        img = soup.select(".col li a img")[number]['src']
        imgName = soup.select(".col li a img")[number]['alt'] + '.jpg'
        img_Url = 'https://www.4kbizhi.com' + img
        response_content = requests.get(url=img_Url, headers=headers).content
        # 拼接名字
        img_path = 'picture/' + imgName
        with open(img_path, 'wb')as fp:
            fp.write(response_content)
        print(imgName, 'over')


# 循环扒取各个页面的图片
def fun2():
    for page in range(14, 15):
        newUrl = format(url % page)
        response = requests.get(newUrl, headers=headers)
        # 注意网页格式
        response.encoding = 'gbk'
        response_text = response.text
        soup = BeautifulSoup(response_text, 'lxml')
        fun1(soup)


def fun4(soup):
    for number in range(1, 5):
        img = soup.select(".slist li a img")[number]['src']
        imgName = soup.select(".slist li a img")[number]['alt'] + '.jpg'
        img_Url = 'https://pic.netbian.com' + img
        response_content = requests.get(url=img_Url, headers=headers).content
        # 拼接名字
        img_path = 'picture1/' + imgName
        with open(img_path, 'wb')as fp:
            fp.write(response_content)
        print(imgName, 'over')


def fun3():
    for page in range(14, 15):
        newUrl = format(url1 % page)
        response = requests.get(newUrl, headers=headers)
        response.encoding = 'gbk'
        response_text = response.text
        soup = BeautifulSoup(response_text, 'lxml')
        fun4(soup)


if __name__ == '__main__':
    print("开始爬取最新高清壁纸")
    fun2()
    print("爬取完成")
    time.sleep(3)
    print("开始爬取彼岸图网")
    fun3()
    print("爬取完成")
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值