以下是使用 Python 爬取 https://m.meishij.net/shicaizuofa/caoyu/ 网站多页图片的示例代码,使用正则表达式提取图片 URL,并将爬取的图片命名为在网站中的名称,然后保存到images文件夹中:

import re
import requests
import os

# 创建保存图片的目录
if not os.path.exists("images"):
    os.makedirs("images")

base_url = "https://m.meishij.net/shicaizuofa/caoyu/"
page = 1
count = 0

while count < 30:
    # 构造当前页的 URL
    url = base_url + f"p{page}/"
    
    # 发送GET请求获取网页内容
    response = requests.get(url)
    html = response.text
    
    # 使用正则表达式提取图片URL和名称
    image_urls = re.findall(r'https://st-cn.meishij.net/r/.+.jpg', html)
    names = re.findall(r'<p class="name">(.+)</p>', html)
    
    for image_url, name in zip(image_urls, names):
        # 下载图片
        response = requests.get(image_url)
        
        # 保存图片到本地,将图片命名为在网站中的名称
        image_name = name + ".jpg"
        with open(f"images/{image_name}", "wb") as f:
            f.write(response.content)
        
        count += 1
        if count >= 30:
            break
    
    page += 1

上述代码中,使用requests库发送HTTP请求获取网页内容。然后使用正则表达式提取图片 URL 和名称,并逐个下载并保存到本地的images文件夹中。图片的命名采用了在网站中的名称。

请确保你已经安装了requests库,你可以使用以下命令进行安装:

pip install requests

运行上述代码后,程序会连续爬取多页的图片,直到达到30张为止。

Python 爬取美食杰菜谱图片:多页爬取、正则表达式提取和图片命名

原文地址: https://www.cveoy.top/t/topic/m4d 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录