通过python脚本批量下载本地文件中的图片文件

1.准备工作
将所有需要下载的url图片地址编辑成一个文件。一行为一个pic地址。
2.将文件保存为pic_data.txt文件
3.将以下代码保存为downloadpic.py
4.执行脚本python downloadpic.py
注意,python代码文件一定要utf编码

在这里插入图片描述

-- coding: utf-8 --

from urllib.request import urlretrieve
import os
import ssl

ssl._create_default_https_context = ssl._create_unverified_context

‘’’
通过txt网址文件,下载图片到本地
‘’’
def download():

# 读取 pic_data.txt 文本内的网址信息
categories = ['pic_data']

for category in categories:

    # 新建存储picture文件夹存储图片
    os.makedirs('picture/%s' % category, exist_ok=True)
    
    # 读取txt文件
    with open('%s.txt' % category, 'r') as file:
        urls = file.readlines()
        
        # 计算链接地址条数
        n_urls = len(urls)

        # 遍历链接地址下载图片
        for i, url in enumerate(urls):
            try:
                 # 请求下载图片,并截取最后链接第一最后一节字段命名图片
                 urlretrieve(url.strip(), 'picture/%s/%s' % (category, url.strip().split('/')[-1]))
                 print('%s %i/%i' % (category, i, n_urls))
            except:
                 print('%s %i/%i' % (category, i, n_urls), 'no image')

if name == ‘main’:
download();

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
### 回答1: 答:import ssldomains = []with open('domains.txt', 'r') as f: for line in f: domains.append(line.strip())for domain in domains: try: cert = ssl.get_server_certificate((domain, 443)) x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert) print(domain + ": " + str(x509.get_notAfter())) except Exception as e: print(domain + ": " + str(e)) ### 回答2: 下面是一个可以从文件逐行读取域名,并批量检查域名的 SSL 证书过期时间的 Python 脚本: ```python import ssl import socket import datetime def check_ssl_expiry(domain): try: hostname = domain.strip() context = ssl.create_default_context() with socket.create_connection((hostname, 443)) as sock: with context.wrap_socket(sock, server_hostname=hostname) as ssock: cert = ssock.getpeercert() expire_date = cert['notAfter'] expire_datetime = datetime.datetime.strptime(expire_date, '%b %d %H:%M:%S %Y %Z') current_datetime = datetime.datetime.now() days_left = (expire_datetime - current_datetime).days print(f'{domain} SSL 证书剩余 {days_left} 天过期') except Exception as e: print(f'{domain} SSL 证书过期时间无法获取: {e}') file_path = "domains.txt" # 域名文件路径 with open(file_path, "r") as file: for line in file: check_ssl_expiry(line) ``` 这个脚本会打开一个文件,其每行包含一个域名。然后,它会依次读取每个域名,并使用 SSL 验证来检查该域名的证书过期时间。如果成功获取到证书过期时间,将会打印出域名和剩余的过期天数。如果获取不到证书过期时间,将会打印出错误信息。 请将域名列表存储在名为 "domains.txt" 的文件,并将其放在与脚本相同的目录下。然后运行脚本,它将逐行读取域名并输出结果。 ### 回答3: 你可以使用Python的`ssl`和`socket`模块来编写一个脚本,从文件读取域名,并批量查找域名的SSL证书过期时间。 首先,你需要创建一个文本文件,其包含一行一个域名。例如,假设你的文件名为`domains.txt`,其内容如下: ``` www.example1.com www.example2.com www.example3.com ``` 然后,你可以使用以下脚本来读取文件的域名,并批量查找其SSL证书的过期时间: ```python import ssl import socket # 打开文件并读取域名 with open('domains.txt', 'r') as file: domains = file.readlines() # 遍历域名列表 for domain in domains: # 去除行尾的换行符 domain = domain.strip() try: # 创建TLS连接 with socket.create_connection((domain, 443)) as sock: with ssl.create_default_context().wrap_socket(sock, server_hostname=domain) as ssock: # 获取证书信息 cert = ssock.getpeercert() # 取出过期时间字段 expire_date = cert['notAfter'] # 打印信息 print(f"域名: {domain}") print(f"SSL证书过期时间: {expire_date}") print() except ssl.CertificateError: print(f"域名: {domain}") print("无法验证证书") print() except ssl.SSLError as e: print(f"域名: {domain}") print(f"SSL错误: {str(e)}") print() ``` 该脚本逐行读取文件,并使用`socket.create_connection`方法与每个域名的443端口建立TLS连接。然后,使用`ssl.create_default_context`和`wrap_socket`方法包装套接字,并获取证书信息。 注意,如果域名具有自签名证书或证书在本地不受信任,则可能会引发`ssl.CertificateError`异常。这段代码也可以捕获其他与SSL相关的错误异常。 通过执行以上脚本,你将看到每个域名以及其SSL证书的过期时间输出在控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是simeon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值