以下是使用Python的代码示例来实现爬取'https://www.ecjtu.edu.cn/'网站的内容,解析数据,并将该网站上的'.jpg'图片下载到本地。

import requests
from bs4 import BeautifulSoup
import os

# 网站URL
url = 'https://www.ecjtu.edu.cn/'

# 发送HTTP请求
response = requests.get(url)
response.encoding = response.apparent_encoding

# 解析HTML页面
soup = BeautifulSoup(response.text, 'html.parser')

# 获取所有的图片链接
image_urls = []
for img in soup.find_all('img'):
    image_url = img.get('src')
    if image_url.endswith('.jpg'):
        image_urls.append(image_url)

# 下载图片到本地
for image_url in image_urls:
    image_name = image_url.split('/')[-1]
    image_path = os.path.join('images', image_name)
    try:
        image_data = requests.get(image_url).content
        with open(image_path, 'wb') as image_file:
            image_file.write(image_data)
        print(f'Successfully downloaded image: {image_name}')
    except:
        print(f'Failed to download image: {image_name}')

上述代码使用requests库发送HTTP请求来获取网站的内容,并使用BeautifulSoup库解析HTML页面。然后,通过查找所有的<img>标签,获取图片链接,并筛选出以'.jpg'结尾的图片链接。最后,使用requests库下载图片,并将其保存到本地的images文件夹中。

请注意,上述代码中使用了os库来创建文件夹和构建图片的完整路径。在运行代码之前,请确保您已经安装了相关的Python库:requestsbeautifulsoup4os。您可以使用以下命令来安装这些库:

pip install requests beautifulsoup4
Python爬取东华理工大学网站图片 - 代码示例及教程

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

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