下面是一个简单的 Python 程序,用于爬取网上的图片:

import requests
import os

url = 'https://www.example.com/images/'
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}

response = requests.get(url, headers=headers)
if response.status_code == 200:
    html = response.text
    urls = []
    for line in html.split('
'):
        if 'img' in line:
            try:
                url = line.split('src='')[1].split(''')[0]
                if 'http' not in url:
                    url = 'https://www.example.com' + url
                urls.append(url)
            except:
                pass
    for url in urls:
        try:
            response = requests.get(url, headers=headers)
            if response.status_code == 200:
                with open(os.path.basename(url), 'wb') as f:
                    f.write(response.content)
        except:
            pass

这个程序首先使用 requests 库获取网页的 HTML 内容,然后使用正则表达式找到所有的图片链接。接着,它从每个链接获取图像,并将其保存在本地文件系统中。

Python 图片爬取教程:快速抓取网络图片

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

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