python3网络爬虫之下载图片

该博客内容展示了如何使用Python的requests和lxml库来抓取网页上的图片链接,并批量下载到本地。代码首先构建了一个包含多个页面URL的列表,然后遍历每个URL,解析HTML获取图片的src属性和alt属性,最后将图片保存到指定文件夹中。整个过程通过设置延时避免了对服务器的过度请求。

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

# -*- coding: UTF-8 *-
import requests
from lxml import etree
import time
import os

# url = "http://www.netbian.com/"
url_list = ["http://www.netbian.com/"]
for i in range(2,100):
    url_list.append("http://www.netbian.com/index_{}.htm".format(i))

for url in url_list:
    response = requests.get(url)

    content = response.content.decode("gbk")

    html_str = ""
    src_list = []
    name_list = []

    html_str = etree.HTML(content)
    # img_list = html_str.xpath("//div[@class='list']/ul/li/a/img")
    src_list = html_str.xpath("//div[@class='list']/ul/li/a/img/@src")
    name_list = html_str.xpath("//div[@class='list']/ul/li/a/img/@alt")
    # print(src_list)
    # print(name_list)

    # for img in img_list:
        # print(img)

    file_path = "./img"

    for i in range(len(src_list)):
        # print(img_list[i])
        # print(name_list[i])
        if os.path.exists(file_path):
            pass
        else:
            os.mkdir(file_path)
        with open(file_path + "/{}.jpg".format(name_list[i]),"wb") as f:
            # f.write(img_list[i])
            time.sleep(0.3)
            res = requests.get(src_list[i])
            con = res.content
            f.write(con)
            print("下载成!")

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值