以下是一个使用Python的示例程序,可以爬取指定网页中的所有图片并保存到本地:

import requests
from bs4 import BeautifulSoup
import os

url = "https://www.thh2023.com/118633.html"

# 发送请求并解析HTML
response = requests.get(url)
soup = BeautifulSoup(response.content, 'html.parser')

# 创建存储图片的文件夹
if not os.path.exists('images'):
    os.makedirs('images')

# 查找所有图片的URL并下载保存
img_tags = soup.find_all('img')
for img_tag in img_tags:
    img_url = img_tag.get('src')
    if img_url.startswith('http'):
        response = requests.get(img_url)
        with open('images/' + os.path.basename(img_url), 'wb') as f:
            f.write(response.content)
            print(img_url, 'saved.')

该程序通过requests库发送GET请求获取指定网页的HTML,然后使用BeautifulSoup库解析HTML,查找所有的img标签,提取img标签中的src属性值,如果这个属性值是以http开头的,就说明这是一个外部链接,程序就会通过requests库发送GET请求下载这张图片并保存到本地。最后,程序会在当前目录下创建一个名为images的文件夹,所有下载的图片都会保存在这个文件夹下。

帮我编写一个爬取网页图片的程序这个网页地址为:httpswwwthh2023com118633html

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

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