以下是一个使用Python爬取https://wallspic.com/cn网站图片的示例代码:

import requests
from bs4 import BeautifulSoup
import os

# 创建一个文件夹用于保存图片
folder_name = 'wallspic_images'
if not os.path.exists(folder_name):
    os.makedirs(folder_name)

# 发送GET请求获取网页内容
url = 'https://wallspic.com/cn'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# 查找所有图片标签并下载图片
image_tags = soup.find_all('img')
for image_tag in image_tags:
    image_url = image_tag['src']
    if image_url.startswith('https://'):
        image_name = image_url.split('/')[-1]
        image_path = os.path.join(folder_name, image_name)
        with open(image_path, 'wb') as f:
            image_response = requests.get(image_url)
            f.write(image_response.content)
            print(f"已下载图片: {image_name}")

这段代码使用了requests库发送GET请求获取网页内容,并使用BeautifulSoup库解析网页内容。然后,它会查找网页中所有的图片标签,并将图片下载到指定的文件夹中。

请注意,爬取网站内容需要遵守网站的使用规则和法律法规。在使用此代码之前,请确保你有权利爬取并下载网站上的图片。

写一个爬取这个网站httpswallspiccomcn的图片的全部代码

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

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