Python爬取彼岸图网10万张高清图片(入门级爬虫)_一蓑烟雨任平生

该博客分享了一个入门级的Python图片爬虫代码。其基本思路是先创建文件夹,再访问网址,用正则爬取图片地址和标题,拼接地址后遍历下载图片。使用时运行代码,输入起始页即可坐等图片下载。

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

来张爬取的美女镇楼

在这里插入图片描述

先上代码,再给你讲解

import re
import requests
import os
import easygui

(min, max) = easygui.multenterbox(fields=['起始数', '终止数'], values=['1', '100'])
min = int(min)
max = int(max)

if os.path.exists('zhiwei'):
    os.chdir('zhiwei')
else:
    os.mkdir('zhiwei')
    os.chdir('zhiwei')
num = 1
for i in range(min, max):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3760.400 QQBrowser/10.5.4083.400'
    }
    urls = "http://pic.netbian.com"
    url = "http://pic.netbian.com/tupian/{}.html".format(str(i))
    res = requests.get(url, headers=headers)
    res.encoding = 'gbk'
    html = res.text
    image = re.findall('<img src="(.*?)" data-pic', html)
    name = re.findall('<h1>(.*?)</h1>', html)
    images = [urls + i for i in image]
    print(images)
    for names in name:
        for img in images:
            file_name = names + '.jpg'
            print("===========================开始下载第{0}张壁纸================================".format(num))
            print(file_name)
            print(img)
            response = requests.get(img)
            with open(file_name, 'wb') as file:
                file.write(response.content)
            print("下载完成")
            num += 1

代码很简单,会一点爬虫的老哥都能看懂,入门级的代码

基本思路

  1. 创建文件夹,判断是否存在,存在就不创建,不存在就新建
if os.path.exists('zhiwei'):
    os.chdir('zhiwei')
else:
    os.mkdir('zhiwei')
    os.chdir('zhiwei')
  1. 访问网址,找到图片位置,利用正则爬取到图片地址(不带前面)还要标题(为啥要爬标题?当然你想把图片命名成123456的话,那你可以不爬名字)
	image = re.findall('<img src="(.*?)" data-pic', html)
    name = re.findall('<h1>(.*?)</h1>', html)
  1. 拼接图片地址(就是加一下网站域名而已)
images = [urls + i for i in image]
  1. 有了文件名称,遍历循环图片名称,根据每次名称去访问图片地址进行下载
response = requests.get(img)
with open(file_name, 'wb') as file:
    file.write(response.content)
print("下载完成")
  1. 然后就完事了

使用方法

运行代码,输入起始页,坐等图片入包

在这里插入图片描述

代码中注解很少,如果看不懂,可以私信我

在这里插入图片描述

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值