欢迎光临UUpython
最大、最新、最全的Python代码收集站

 通过右键生成链接在局域网内分享文件

uupython阅读(779)

这段代码是一个使用Flask创建文件下载服务器的脚本,并在文件右键菜单中添加了“分享此文件”的选项。它能够将指定的文件通过HTTP提供下载,并在本地生成一个随机端口的服务器。

以下是代码的主要功能和流程:

  1. 导入所需的模块:导入了Flask、send_from_directory、os、sys、netifaces、random、pyperclip、winreg等模块。
  2. 定义启动Flask服务器的函数:start_flask_server函数用于启动Flask服务器。该服务器提供文件下载服务。
  3. 获取物理网卡IP:get_wired_ip函数使用netifaces库获取物理网卡的IP地址。
  4. 写入注册表项:write_registry_entry函数用于写入注册表项,以添加“分享此文件”的右键菜单选项。
  5. 读取注册表值:read_registry_value函数用于读取注册表值,以获取Python的执行路径。
  6. 主程序入口:在if __name__ == '__main__':中,首先获取命令行参数和Python可执行路径,然后写入注册表项。
  7. 如果没有命令行参数:如果没有传入文件参数,提示用户通过右键菜单运行程序,并等待用户输入回车键退出程序。
  8. 如果有命令行参数:如果有传入文件参数,获取文件路径、文件名、物理网卡IP和随机端口。然后在终端打印文件下载地址,并将地址复制到剪贴板。
  9. 启动Flask服务器:调用start_flask_server函数启动Flask服务器,提供文件下载服务。

这段代码实现了一个方便的文件分享工具,可以在右键菜单中快速分享指定的文件。用户右键选择文件,点击“分享此文件”,即可在终端获取文件的下载地址,然后可以将下载地址分享给其他人。需要注意的是,此脚本需要在Windows系统上运行。

# -*- coding: utf-8 -*-
 
from flask import Flask, send_from_directory
import os
import sys
import netifaces
import random
import pyperclip
import winreg
 
def start_flask_server(p,fol):
    app = Flask(__name__)
 
    @app.route('/download/<filename>', methods=['GET'])
    def download_file(filename):
        file_path = os.path.join(fol, filename)
        if not os.path.exists(file_path):
            return "文件不存在", 404
        return send_from_directory(fol, filename, as_attachment=True, conditional=True)
          
    app.run(host='0.0.0.0', port=p, debug=False, threaded=True)
 
def get_wired_ip():
    try:
        interfaces = netifaces.interfaces()
        for interface in interfaces:
            addresses = netifaces.ifaddresses(interface)
            if netifaces.AF_INET in addresses:
                for address in addresses[netifaces.AF_INET]:
                    ip_address = address['addr']
                    if ip_address != '127.0.0.1':
                        return ip_address
    except (KeyError, IndexError):
        pass
    return "未找到物理网卡IP"
 
def write_registry_entry(epath, ico):
    reg_path = r"*\shell\Share File"
    key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, reg_path)
    winreg.SetValue(key, None, winreg.REG_SZ, "分享此文件")
    winreg.SetValueEx(key, 'ICON', 0, winreg.REG_SZ, ico)
    winreg.CloseKey(key)
 
    command_path = r"*\shell\Share File\command"
    command_key = winreg.CreateKey(winreg.HKEY_CLASSES_ROOT, command_path)
    winreg.SetValue(command_key, None, winreg.REG_SZ, epath + ' "%1"')
    winreg.CloseKey(command_key)
 
def read_registry_value():
    try:
        key = winreg.OpenKey(winreg.HKEY_CLASSES_ROOT, r"Python.File\Shell\open\command")
        value, _ = winreg.QueryValueEx(key, "")
        winreg.CloseKey(key)
        value = value.split('"')
        return value[1]
    except OSError:
        print("注册表项不存在")
    except Exception as e:
        print(f"读取注册表值时出现错误: {e}")
 
 
if __name__ == '__main__':
    args = sys.argv
    executable_path = os.path.abspath(sys.executable)
    if getattr(sys, 'frozen', False):
        exe = icon = '"' + executable_path + '"'
    else:
        pypath = read_registry_value()
        exe = '"' + pypath + '" "' + args[0] + '"'
        icon = '"' + pypath + '"'
 
    write_registry_entry(exe,icon)
 
    if len(args) - 1==0:
        print("请通过右键菜单运行此程序")
        input("按回车键退出程序")
        sys.exit(0)
    file = args[1]
    path = os.path.dirname(file)
    filename = os.path.basename(file)
    ip = get_wired_ip()
    port = random.randint(10000, 65500)
    url = "http://" + ip + ":" + str(port) + "/download/" + filename
    print("\n↓↓文件下载地址已复制到剪贴板↓↓\n\n " + url + " \n")
    print("↑↑文件下载地址已复制到剪贴板↑↑\n\n\n52pojie\n")
 
    pyperclip.copy(url)
    start_flask_server(port,path)
    

生成校验软件的注册密钥

uupython阅读(775)

这段代码是一个简单的Python脚本,用于生成和校验软件的注册密钥。脚本使用了wmi库来获取计算机的CPU序列号,并使用hashlib库进行MD5哈希加密。

以下是代码的主要功能和流程:

  1. 定义常量和文件路径:
  • AUTHOR:作者或标识,用于生成密钥。
  • KEYFILENAME:存储密钥的文件名。
  1. 获取本地机器的加密字符串:
  • 使用wmi库获取计算机的CPU序列号,然后将其与AUTHOR拼接。
  • 对拼接后的字符串进行一次MD5哈希加密。
  • 将加密结果与AUTHOR再次拼接,进行第二次MD5哈希加密。
  • 返回最终的加密字符串。
  1. 从文件中获取密钥:
  • 打开存储密钥的文件,读取第一行内容作为密钥。
  • 返回读取的密钥字符串。
  1. 校验是否注册:
  • 获取本地机器的加密字符串。
  • 从文件中获取密钥。
  • 将文件中的密钥与本地机器的加密字符串进行比较。
  • 如果比较结果相等,则返回True表示已注册,否则返回False表示未注册。
  1. 生成注册密钥文件:
  • 获取CPU序列号。
  • 将CPU序列号与AUTHOR拼接,并进行一次MD5哈希加密。
  • 将加密结果写入存储密钥的文件。
  1. 生成注册密钥文件(重载函数):
  • 接受一个解密字符串参数。
  • 将解密字符串与AUTHOR拼接,并进行一次MD5哈希加密。
  • 将加密结果写入存储密钥的文件。
  1. 显示CPU序列号:
  • 使用wmi库获取计算机的CPU序列号。

这段代码可以用于生成和验证软件的注册密钥,通过对CPU序列号的加密和校验,实现了简单的注册功能。在使用之前,需要替换AUTHOR为相应的标识,然后根据需要调用相应的函数。

import wmi
import hashlib
import os
 
AUTHOR='52pj'
KEYFILENAME='./RegisterKey.key'
 
#获取CPU的序列号加上Author字段,MD5一次,再在结果加上Author字段再MD5一次,形成最终的比较密码
def GetLocalMachineEncrypedString():
    try:
        c = wmi.WMI()
        decryptString = c.win32_Processor()[0].ProcessorId.strip() + AUTHOR
        encrypedString = hashlib.md5(decryptString.encode('utf-8')).hexdigest()
        decryptString = encrypedString + AUTHOR
        encrypedString = hashlib.md5(decryptString.encode('utf-8')).hexdigest()
    except:
        encrypedString = hashlib.md5(AUTHOR.encode('utf-8')).hexdigest()
    return encrypedString
 
#文件中储存的Key是CPU序列号加Author,一次MD5结果
def GetKeyFromFile():
    try:
        f=open(KEYFILENAME,'r')
        lines=f.readlines()
        keyString=lines[0]
        return keyString
    except:
        return ''
 
#把文件的key加上author,做最后一次MD5生成最终结果
def CheckIsRegistered():
    decryptString=GetKeyFromFile()+AUTHOR
    encrypedString = hashlib.md5(decryptString.encode('utf-8')).hexdigest()
    if encrypedString==GetLocalMachineEncrypedString():
        return True
    else:
        return False
 
#生成key文件
def BuildRegisterKey():
    try:
        c = wmi.WMI()
        decryptString = c.win32_Processor()[0].ProcessorId.strip() + AUTHOR
        encrypedString = hashlib.md5(decryptString.encode('utf-8')).hexdigest()
    except:
        encrypedString=''
 
    try:
        f=os.open(KEYFILENAME,os.O_CREAT|os.O_WRONLY)
        os.write(f,encrypedString.encode('utf-8'))
        print('写入', KEYFILENAME, '成功')
    except:
        print('写入',KEYFILENAME,'失败')
 
def BuildRegisterKey(decryptString):
    if decryptString:
        decryptString = decryptString + AUTHOR
        encrypedString = hashlib.md5(decryptString.encode('utf-8')).hexdigest()
    else:
        encrypedString=''
 
    try:
        f=os.open(KEYFILENAME,os.O_CREAT|os.O_WRONLY)
        os.write(f,encrypedString.encode('utf-8'))
        print('写入', KEYFILENAME, '成功')
    except:
        print('写入',KEYFILENAME,'失败')
 
def ShowCPUSerialNumber():
    try:
        c = wmi.WMI()
        decryptString = c.win32_Processor()[0].ProcessorId.strip()
    except:
        decryptString=''
    return decryptString

爬取cnn新闻

uupython阅读(1011)

这段代码是一个Python脚本,用于从CNN新闻网站抓取新闻链接,然后使用OpenAI的GPT-3.5模型生成摘要并将其保存到文本文件中。代码使用了 requests 库来发送HTTP请求,BeautifulSoup 来解析HTML内容,以及OpenAI的API来生成摘要。

以下是代码的主要功能和流程:

  1. 导入所需的模块:
  • os:用于操作文件系统路径。
  • datetime:用于获取当前日期。
  • requests:用于发送HTTP请求和获取网页内容。
  • BeautifulSoup:用于解析HTML页面内容。
  • urljoin:用于构建绝对URL。
  • openai:用于访问OpenAI的API。
  • time:用于控制请求频率。
  1. 设置OpenAI的API密钥:
  • 在代码中设置OpenAI的API密钥。
  1. 获取当前日期并创建文件夹:
  • 使用datetime.date.today()获取当前日期。
  • 构建文件夹路径,并使用os.makedirs创建文件夹。
  1. 构建文件路径和初始URL:
  • 构建要保存文件的路径。
  • 设置CNN新闻网站的初始URL。
  1. 发送请求并解析HTML内容:
  • 使用requests.get获取CNN新闻网站的HTML内容。
  • 使用BeautifulSoup解析HTML内容,找到新闻链接的容器。
  1. 遍历链接并生成摘要:
  • 遍历获取到的新闻链接。
  • 对每个链接,发送HTTP请求,获取文章内容并解析HTML。
  • 提取文章内容,构建用户输入,然后使用OpenAI的ChatCompletion生成摘要。
  1. 控制请求频率:
  • 使用time.sleep控制每个请求的时间间隔,以控制请求频率。
  1. 将摘要写入文件并打印:
  • 将生成的摘要写入文本文件。
  • 打印生成的摘要。

运行脚本前,确保已安装了需要的库(requestsBeautifulSoupopenai),并替换API密钥。另外,注意不要过度访问网站或OpenAI API,以遵守相关的使用条款和规定。

import os
import datetime
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import openai
import time
 
# 在这里设置你的 OpenAI API 密钥
openai.api_key = ''
 
# 获取当前日期
current_date = datetime.date.today()
 
# 创建文件夹路径
folder_path = os.path.join("C:/桌面/每日新闻", str(current_date))
 
# 创建文件夹
os.makedirs(folder_path, exist_ok=True)
 
# 创建文件路径
file_path = os.path.join(folder_path, "CNN新闻.txt")
 
url = "https://edition.cnn.com/"
 
response = requests.get(url)
html_content = response.content
 
soup = BeautifulSoup(html_content, "html.parser")
 
container = soup.find(class_="container__field-links container_ribbon__field-links")
 
if container:
    links = container.find_all("a")
 
    # 打开文件并写入内容
    with open(file_path, "w", encoding="utf-8") as file:
        # 遍历链接并访问每个链接
        for link in links:
            href = link.get("href")
            full_link = urljoin(url, href)
 
            try:
                response = requests.get(full_link)
                response.raise_for_status()  # 检查是否有异常状态码
                html = BeautifulSoup(response.content, "html.parser")
 
                articles = html.find_all(class_="article__content")
 
                if articles is None:
                    continue
 
                content = ' '.join([article.get_text() for article in articles])
 
                user_input = f"摘要以下文章内容:\n{content}\n摘要:"
 
                # 控制请求频率
                time_between_requests = 60 / 3  # 3 RPM
                time.sleep(time_between_requests)
 
                summary_response = openai.ChatCompletion.create(
                    model="gpt-3.5-turbo",
                    messages=[
                        {"role": "system", "content": "You are a helpful assistant."},
                        {"role": "user", "content": user_input}
                    ],
                    temperature=1,
                    max_tokens=256,
                )
 
                summary = summary_response.choices[0].message['content'].strip()
 
                # 将摘要写入文件
                file.write(summary + "\n\n")
 
                # 打印摘要
                print(summary)
                print('---------------------------------------------------------------------------------')
 
            except requests.RequestException as e:
                print(f"请求出错:{str(e)}")
 
    print("文件写入完成!")

从小说阅读网站上抓取小说的章节内容

uupython阅读(741)

这段代码是一个简单的爬虫脚本,用于从小说阅读网站上抓取小说的章节内容。脚本使用了 requests 库来发送HTTP请求,使用 BeautifulSoup 来解析HTML内容,以及使用正则表达式来匹配章节目录。

以下是代码的主要功能和流程:

  1. 导入所需的模块:
  • requests:用于发送HTTP请求和获取网页内容。
  • re:用于正则表达式匹配。
  • BeautifulSoup:用于解析HTML页面内容。
  1. 定义 get_soup 函数:
  • 通过给定的URL和参数,发送GET请求并获取网页内容。
  • 使用 BeautifulSoup 解析网页内容,并返回解析后的BeautifulSoup对象。
  1. 定义 get_chapter_content 函数:
  • 通过给定的章节URL,获取章节内容。
  • 使用正则表达式匹配章节内容,并去除不需要的部分。
  • 返回章节内容。
  1. 定义 main 函数:
  • 定义一个小说名字列表 namelist
  • 遍历小说名字列表,对每本小说进行爬取。
  • 通过小说名字搜索小说链接,获取小说章节目录。
  • 对每个章节链接,循环尝试获取章节内容,并保存到对应的文本文件。

此脚本通过爬取小说阅读网站,根据小说名字搜索小说链接,获取小说章节目录,然后逐个爬取章节内容并保存到对应的文本文件中。运行脚本前,请确保已安装了 requestsBeautifulSoupre 库。另外,请注意网站的使用条款和规定,避免过度访问和爬取。

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# time: 2023/7/25 15:22
# file: xbiquge.py
# author: lyx
 
import requests
import re
from bs4 import BeautifulSoup
 
def get_soup(url, params=None):
    res = requests.get(url=url, params=params)
    res.encoding = 'utf-8'
    return BeautifulSoup(res.text, 'html.parser')
 
def get_chapter_content(url):
    chapter = requests.get(url)
    chapter.encoding = 'utf-8'
    chapter1 = BeautifulSoup(chapter.text, 'html.parser')
    chapter_div = chapter1.find('div', {'id': 'content'})
    if chapter_div:
        paragraphs = chapter_div.find_all('p')
        for p in paragraphs:
            p.decompose()
        div = chapter_div.find_all('div')
        for d in div:
            d.decompose()
    return chapter_div
 
def main():
    namelist = ['阵问长生','神明模拟器','半岛检查官','全民逃荒,我的物品能升级','玄鉴仙族']
    for bookname in namelist:
        print(f"爬取书籍: {bookname}!")
        url = 'https://www.ibiquges.info/modules/article/waps.php'
        data = {'searchkey': bookname}
        soup = get_soup(url, data)
        content = soup.find('a', string=bookname)
 
        if content:
            url1 = 'https://www.ibiquges.info'
            url = content['href']
            content = get_soup(url)
            catalog1 = content.find('div', id='list')
            catalog2 = str(catalog1)
            pattern = r'<dd><a href="(.*?)">(.*?)</a></dd>'
            matches = re.findall(pattern, catalog2)
            result = [(match[0], match[1]) for match in matches]
            with open(bookname + '.txt', 'a', encoding='utf-8') as file:
                for chapter_url, chapter_title in result:
                    for _ in range(20):
                        url2 = url1 + str(chapter_url)
                        chapter_div = get_chapter_content(url2)
                        if chapter_div is not None:
                            middle_text = chapter_div.get_text("\n", strip=True)
                            print('\n\n\n' + chapter_title + '\n\n\n')
                            file.write('\n\n\n' + chapter_title + '\n\n\n')
                            file.write(middle_text)
                            break
                        else:
                            continue
        else:
            print("未找到匹配的URL")
 
if __name__ == "__main__":
    main()

获取中国新闻网的资讯归档

uupython阅读(247)

这段代码是一个简单的Python脚本,用于从中国新闻网上抓取新闻标题、栏目和时间信息,并将这些信息存储到一个Excel文件中。代码使用了requests库来发送HTTP请求,BeautifulSoup库来解析HTML内容,以及openpyxl库来操作Excel文件。

以下是代码的主要功能和流程:

  1. 导入所需的模块:
  • requests:用于发送HTTP请求和获取网页内容。
  • BeautifulSoup:用于解析HTML页面内容。
  • openpyxl 中的 Workbook 类:用于创建Excel工作簿。
  1. 获取当前时间并格式化:
  • 使用datetime模块获取当前时间。
  • 使用strftime方法将时间格式化为指定的字符串格式,以便作为Excel文件名的一部分。
  1. 创建Excel工作簿和Sheet页:
  • 使用Workbook类创建一个Excel工作簿。
  • 添加一个Sheet页并指定名称为’Sheet1’。
  1. 定义表头:
  • 使用sheet['A1']sheet['B1']sheet['C1']设置表头内容。
  1. 遍历不同页的新闻信息:
  • 使用一个循环遍历不同页码的新闻页面。
  • 构建URL并发送GET请求获取网页内容。
  1. 使用BeautifulSoup解析HTML内容:
  • 解析网页内容,提取栏目、标题和时间的信息。
  1. 循环遍历新闻数据并写入Excel表格:
  • 使用一个循环遍历每个新闻条目的栏目、标题和时间信息。
  • 使用sheet.append将这些信息逐行添加到Excel表格中。
  1. 保存Excel文件:
  • 使用wb.save方法将Excel工作簿保存为一个Excel文件,文件名中包含格式化后的时间。

此脚本通过爬取中国新闻网的新闻页面,将抓取到的新闻标题、栏目和时间信息存储到一个Excel文件中。运行此脚本前,请确保已安装了 requestsBeautifulSoupopenpyxl 库。另外,根据需要可以修改代码中的URL和其他参数。

import requests
from bs4 import BeautifulSoup
from openpyxl import Workbook
from datetime import datetime

# -----参考文档,三件套-------
# https://docs.python-requests.org/en/latest/
# https://www.crummy.com/software/BeautifulSoup/bs4/doc/
# https://openpyxl.readthedocs.io/en/stable/
# https://docs.python.org/3/library/stdtypes.html#str.strip (切片)
# -----参考文档,三件套-------


# 5.1 新增时间格式规范化输出文件名
# 获取当前时间
now = datetime.now()
# 将时间格式化为指定的字符串格式
# formatted_time = now.strftime('%Y-%-m-%-d')
formatted_time = now.strftime('%Y-%m-%d')

# 创建一个Workbook对象,用于Excel的读写
wb = Workbook()

# 添加一个Sheet页,并且指定Sheet名称
sheet = wb.active
sheet.title = 'Sheet1'

# 定义变量row,用于循环时控制每一行的写入位置
row = 1

# 添加表头
sheet['A1'] = '栏目'
sheet['B1'] = '标题'
sheet['C1'] = '时间'

# 遍历页码从1到2页
for page_num in range(1,3):
    # f-string
    url = f"https://www.chinanews.com.cn/scroll-news/news{page_num}.html"

    # 反爬通用套码
    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'
    }

    r = requests.get(url, headers=headers)
    r.encoding='utf-8'
    soup = BeautifulSoup(r.text, 'html.parser')

    # 遍历栏目、标题和时间
    dangdu_lanmu = soup.find_all('div', class_='dd_lm')
    dangdu_biaoti = soup.find_all('div', class_='dd_bt')
    dangdu_time = soup.find_all('div', class_='dd_time')
    
    # 追加具体数据
    for news_num in range(len(dangdu_lanmu)):
        sheet.append([dangdu_lanmu[news_num].text.strip('[]'), dangdu_biaoti[news_num].text, dangdu_time[news_num].text])
        # row=row+1
        row += 1

# 保存Excel文件
wb.save("chinanews_{}.xlsx".format(formatted_time))

手写拍照转换为电子签名,背景透明图

uupython阅读(308)

这段代码是一个用于处理图片的Python脚本,它使用了PIL(Python Imaging Library)库来进行图片处理操作。主要功能是将指定颜色范围之外的像素设为透明,实现了一个简单的抠图效果。

以下是代码的主要功能和流程:

  1. 导入所需的模块:
  • sys:用于获取命令行参数。
  • PIL 中的 Image 模块:用于图片的打开、处理和保存。
  1. clip_image 函数:
  • 打开指定的图片,将其转换为 RGBA 模式,其中 A 表示 alpha 通道(透明度通道)。
  • 遍历图片的每个像素,根据像素的 RGB 值来判断是否应该设为透明或不透明。
  • 最终将处理后的图片保存。
  1. 主函数:
  • 如果脚本被命令行执行,则会读取命令行传入的图片文件路径。
  • 对每个传入的图片路径,调用 clip_image 函数进行处理,并保存处理后的图片。

运行此脚本时,可以将图片文件拖动到脚本可执行文件上,然后脚本会将图片的部分区域抠图,并将抠图后的图片保存。代码中的阈值条件可以根据需要进行调整,以满足不同情况下的抠图需求。

请注意,此脚本涉及图片处理操作,请确保你已经安装了 PIL(Pillow)库,可以使用以下命令安装:

pip install Pillow
# coding=utf8
import sys
from PIL import Image
 
def clip_image(image_path, save_path):
    img = Image.open(img_path)
    img = img.convert('RGBA')
    x,y=img.size
    for i in range(x):
        for j in range(y):
            xiangsu=img.getpixel((i,j))
            if xiangsu[0]>100 or xiangsu[1]>100 or xiangsu[2]>100:
                img.putpixel((i, j), (0,0,0,0))
            else:
                img.putpixel((i,j), (0,0,0,255) )
 
    img.save(save_path)
 
if __name__=='__main__':
    if len(sys.argv)<2:
        input('不能直接运行,需要把图片文件拖动到可执行程序上来')
        exit(0)
    total=len(sys.argv)-1
    print("总任务数:%d"%total)
    for i in range(1,total +1):
        img_path=sys.argv[i]
        print('当前要处理的图片:', img_path)
        save_path = img_path.split(r'/')[-1] + "_ele_sign.png"
        clip_image(img_path, save_path)
        print('处理后保存的位置:', save_path)
    input('所有任务已处理完毕,按任意键退出')

anime-pictures好看壁纸爬虫

uupython阅读(367)

这段代码是一个简单的Python脚本,用于从特定网站上下载图片。代码使用了requests库来发送HTTP请求,使用BeautifulSoup库来解析HTML内容,并通过线程池来并发下载图片。

以下是代码的主要功能和流程:

  1. 导入所需的模块:
  • requests:用于发送HTTP请求和获取网页内容。
  • BeautifulSoup:用于解析HTML页面内容。
  • os:用于操作文件和文件夹。
  • urllib.parse:用于拼接URL。
  • ThreadPoolExecutoras_completed:用于并发执行下载任务。
  • Tkfiledialog:用于选择保存路径的图形界面组件。
  1. download_image 函数:
  • 下载图片并保存到指定路径。
  • 在下载之前,检查图片大小,如果太小则跳过下载。
  • 将下载过的文件名记录在日志文件中。
  1. download_images 函数:
  • 根据给定的URL,解析网页内容,提取图片链接并下载图片。
  • 使用线程池来并发下载图片。
  • 通过 page 计数来翻页下载图片。
  1. 主函数:
  • 弹出文件夹选择对话框,选择保存路径。
  • 如果选择了保存路径,则调用 download_images 函数下载图片。

此脚本适用于下载网站上的图片,并使用线程池来提高下载效率。在运行之前,请确保已安装所需的库,如 requestsBeautifulSoupThreadPoolExecutor。另外,将代码中的url替换为你想要下载图片的网站链接。

需要注意的是,爬取网站内容时请遵循网站的使用条款和规定,不要对网站进行不正当的访问和下载。

import requests
from bs4 import BeautifulSoup
import os
import urllib.parse
from concurrent.futures import ThreadPoolExecutor, as_completed
from tkinter import Tk, filedialog
 
def download_image(img_info, log_file_path):
    img_url, image_path = img_info
    image_name = os.path.basename(image_path)
 
    # 追加写入log.txt
    with open(log_file_path, 'a') as log_file:
        log_file.write(f'{image_name},')
    if os.path.exists(image_path):
        print(f'Skipped image: {image_name}')
    else:
        # 获取文件大小
        response = requests.head(img_url)
        file_size = int(response.headers.get('Content-Length', 0))
         
        if file_size < 200 * 1024:  # 200KB
            print(f'Skipped image: {image_name} (File size too small)')
        else:
            image_data = requests.get(img_url).content
            with open(image_path, 'wb') as f:
                f.write(image_data)
            print(f'Downloaded image: {image_name}')
 
         
 
def download_images(url, save_path):
    # 创建保存图片的文件夹
    os.makedirs(save_path, exist_ok=True)
 
    page = 0
    log_file_path = os.path.join(save_path, 'log.txt')
 
    # 读取已下载的文件列表
    downloaded_files = []
    if os.path.exists(log_file_path):
        with open(log_file_path, 'r') as log_file:
            downloaded_files = log_file.read().split(',')
 
    with ThreadPoolExecutor(max_workers=2) as executor:
        while True:
            page_url = url + f'?page={page}&lang=en'
 
            response = requests.get(page_url)
            soup = BeautifulSoup(response.text, 'html.parser')
 
            img_tags = soup.find_all('img', class_='svelte-1ibbyvk')
 
            if not img_tags:
                break
 
            download_tasks = []
            for img_tag in img_tags:
                img_src = img_tag['src']
                img_path = img_src.split('previews/')[1].replace('_cp', '')
                img_url = urllib.parse.urljoin('https://images.anime-pictures.net/', img_path)
 
                image_name = os.path.basename(img_path)
                image_path = os.path.join(save_path, image_name)
 
                if image_name not in downloaded_files:
                    download_tasks.append((img_url, image_path))
 
            for future in as_completed(executor.submit(download_image, task, log_file_path) for task in download_tasks):
                try:
                    future.result()
                except Exception as e:
                    print(f'Error occurred: {str(e)}')
 
            page += 1
            print(f'当前下载第: {page}页')
 
# 使用文件夹选择对话框选择保存路径
root = Tk()
root.withdraw()
save_path = filedialog.askdirectory(title='选择保存路径')
 
if save_path:
    url = 'https://anime-pictures.net/posts'  # 替换为你的链接
    download_images(url, save_path)

出题系统,每个学生的都不一样

uupython阅读(224)

这段代码是一个试卷生成器,它使用Python编写,并利用了第三方库来生成随机的试题和答案,并将它们保存到Word文档中。下面是代码的主要功能和流程:

  1. 导入所需的模块:
  • random:用于生成随机数。
  • openpyxl:用于读取Excel文件。
  • DocumentPtqn:用于创建和格式化Word文档,来自docx模块。
  1. Random_num 函数:
  • 根据给定的最大数 num_max 和生成数量 num_qty,生成一组不重复的随机整数。
  • 这些随机整数将用作题目的编号,以从题库中选取对应的题目。
  1. Question 函数:
  • 根据题目类型和题目编号,从 Excel 文件中的题库抽取相应的题目。
  • 对于单选题和多选题,会抽取问题、选项和正确答案等信息。
  • 对于判断题和填空题,只抽取问题和正确答案。
  1. To_word 函数:
  • 将抽取的题目数据写入一个 Word 文档模板中,分别按题型分段落写入。
  • 每个题目的题干和选项会被格式化为文本,并插入到文档中。
  1. Answer 函数:
  • 生成答案文档,将抽取的题目答案按照题型分段落写入。
  • 答案也会按照题号进行编号。
  1. 主函数:
  • 通过循环生成多套试卷,每套试卷包括随机选取的单选题、多选题、判断题和填空题。
  • 使用上述函数生成试题和答案,并保存为不同的 Word 文档。

这段代码的功能是自动生成多套试卷,每套试卷包含随机选取的题目,包括单选题、多选题、判断题和填空题,以及对应的答案。它通过读取题库 Excel 文件和一个试卷模板 Word 文档来完成生成。要使用这段代码,你需要准备好题库 Excel 文件和试卷模板 Word 文档,并安装相应的库(randomopenpyxldocx)。另外,该代码在生成试题的过程中,使用了硬编码的题库文件名和模板文件名,你需要根据实际情况进行调整。

import random
#不重复随机整数生成函数
def Random_num(num_max, num_qty):
    '''
    num_max:最大数
    num_qty:生成随机数的个数
    '''
    num_list = [] #储存生成的随机数
    while len(num_list) < num_qty: #控制随机数的个数
        num = random.randint(2,num_max) #设定在此范围内取数
        if num in num_list: #判断随机数是否重复
            continue #若重复,则重新生成
        else:
            num_list.append(num) #将不重复的随机数放入列表
    return num_list #生成完成后返回随机数列表
 
#定义函数,按随机数在题库中抽取对应编号的题目
from openpyxl import load_workbook
def Question(que_type,numbers):
    '''
    que_type:试题类型("单选题","多选题","判断题","填空题")
    numbers:需要抽取的试题编号
    '''
    questions = [] #储存抽取的题目
    wb = load_workbook("题库.xlsx") #载入题库
    if que_type == "单选题":
        ws = wb[que_type]        
        for i in numbers: #按随机生成的编号抽题
            question = ws["B"+str(i)].value #问题在B列
            answerA = "A:\t" + str(ws["C"+str(i)].value) #选项A在C列,"\t"相当于按一下tab键,在字符间产生间隔
            answerB = "B:\t" + str(ws["D"+str(i)].value) #选项B在D列
            answerC = "C:\t" + str(ws["E"+str(i)].value) #选项C在E列
            answerD = "D:\t" + str(ws["F"+str(i)].value) #选项D在F列
            right_answer  = ws["G"+str(i)].value #正确答案在G列
            single_question = [question, answerA, answerB, answerC, answerD, right_answer] #每行的数据存入列表
            questions.append(single_question) #每个题目的数据存入总列表
    elif que_type == "多选题":
        ws = wb[que_type]        
        for i in numbers:
            question = ws["B"+str(i)].value            
            answerA = "A:\t" + str(ws["C"+str(i)].value)
            answerB = "B:\t" + str(ws["D"+str(i)].value)
            answerC = "C:\t" + str(ws["E"+str(i)].value)
            answerD = "D:\t" + str(ws["F"+str(i)].value)
            right_answer  = ws["H"+str(i)].value
            single_question = [question, answerA, answerB, answerC, answerD, right_answer]
            if ws["G"+str(i)].value: #有些题有E选项,有些没有,因此需要判断一下是否有E选项
                answerE = "E:\t" + str(ws["G"+str(i)].value)
                single_question.insert(-1,answerE) #将E选项插入到答案前面,保持答案是最后一个元素
            questions.append(single_question)
    else: #判断题和填空题,内容只取题干和答案
        ws = wb[que_type]        
        for i in numbers:
            question = ws["B"+str(i)].value
            right_answer  = ws["C"+str(i)].value
            single_question = [question, right_answer]
            questions.append(single_question)
             
    return questions
 
#写入考试题到word文件
from docx import Document
from docx.shared import Pt #用于设定字体大小(磅值)
from docx.oxml.ns import qn #用于应用中文字体
def To_word(number,questions_data):
    doc = Document("试题-模板.docx")
 
    #写入单选题
    title1 = "一、单项选择题(共40题,每题1分)"
    p = doc.add_paragraph() #插入段落
    r = p.add_run(title1) #插入文字块
    r.bold = True #字体加粗
    r.font.size = Pt(12) #字号设为12磅
    for index, i in enumerate(questions_data["单选题"],start = 1): #给题目从1开始编号
        doc.add_paragraph(f"{index}. {i[0]}") #题干部分在单独一段
        doc.add_paragraph(f"\t{i[1]}\t\t{i[2]}") #选项A和选项B在同一段落
        doc.add_paragraph(f"\t{i[3]}\t\t{i[4]}") #选项C和选项D在同一段落
 
    #写入多选题
    title2 = "二、多项选择题(共20题,每题2分)"
    p = doc.add_paragraph()
    r = p.add_run(title2)
    r.bold = True
    r.font.size = Pt(12)
    for index, i in enumerate(questions_data["多选题"],start = 1):
        doc.add_paragraph(f"{index}. {i[0]}")
        doc.add_paragraph(f"\t{i[1]}\t\t{i[2]}")
        doc.add_paragraph(f"\t{i[3]}\t\t{i[4]}")
        if len(i) ==7: #判断是否有E选项,若有,则新建一段落写入
            doc.add_paragraph(f"\t{i[5]}")
 
    #写入判断题
    title3 = "三、判断题(共10题,每题1分)"
    p = doc.add_paragraph()
    r = p.add_run(title3)
    r.bold = True
    r.font.size = Pt(12)
    for index, i in enumerate(questions_data["判断题"],start = 1):
        doc.add_paragraph(f"\t{index}. {i[0]}")
 
    #写入填空题
    title4 = "四、填空题(共10题,每题1分)"
    p = doc.add_paragraph()
    r = p.add_run(title4)
    r.bold = True
    r.font.size = Pt(12)
    for index, i in enumerate(questions_data["填空题"],start = 1):
        doc.add_paragraph(f"\t{index}. {i[0]}")
 
    doc.save(f"试卷及答案\\考试题{number}.docx")
 
#写入答案
from docx import Document
from docx.shared import Pt #用于设定字体大小(磅值)
from docx.oxml.ns import qn #用于应用中文字体
 
def Answer(number,questions_data):
    doc = Document()
    #全局字体设为“宋体”
    doc.styles['Normal'].font.name=u'宋体'
    doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
     
    title = "计算机系2020第二学期期末考试题(答案)"
    p = doc.add_paragraph()
    r = p.add_run(title)
    r.bold = True
    r.font.size = Pt(20)
    #写入单选题答案
    title1 = "一、单项选择题答案(共40题,每题1分)"
    p = doc.add_paragraph()
    r = p.add_run(title1)
    r.bold = True
    r.font.size = Pt(12)
     
    p = doc.add_paragraph()
    for index, i in enumerate(questions_data["单选题"],start = 1):
        p.add_run(f"{index}. {i[-1]}\t")
        if index%10 == 0: #每段只显示10个答案
            p = doc.add_paragraph() #满10个,则新建段落
 
    #写入多选题答案
    title2 = "二、多项选择题答案(共20题,每题2分)"
    p = doc.add_paragraph()
    r = p.add_run(title2)
    r.bold = True
    r.font.size = Pt(12)
    p = doc.add_paragraph()
    for index, i in enumerate(questions_data["多选题"],start = 1):
        p.add_run(f"{index}. {i[-1]}\t")
        if index%3 == 0: 
            p = doc.add_paragraph() 
         
    #写入判断题答案
    title3 = "三、判断题答案(共10题,每题1分)"
    p = doc.add_paragraph()
    r = p.add_run(title3)
    r.bold = True
    r.font.size = Pt(12)
    p = doc.add_paragraph()
    for index, i in enumerate(questions_data["判断题"],start = 1):
        p.add_run(f"{index}. {i[-1]}\t")
        if index%5 == 0: #每段只显示5个答案
            p = doc.add_paragraph() #满5个,则新建段落
 
    #写入填空题
    title4 = "四、填空题答案(共10题,每题1分)"
    p = doc.add_paragraph()
    r = p.add_run(title4)
    r.bold = True
    r.font.size = Pt(12)
    p = doc.add_paragraph()
    for index, i in enumerate(questions_data["填空题"],start = 1):
        p.add_run(f"{index}. {i[-1]}\t\t")
        if index%2 == 0: #每段只显示2个答案
            p = doc.add_paragraph() #满2个,则新建段落
             
    doc.save(f"试卷及答案\\考试题{number}答案.docx")
 
#主函数
for number in range(1,21): #不同的试卷数量,此处为20套
    #生成随机题目编号    
    num_single_choice = Random_num(566,40)
    num_mult_choice = Random_num(196,20)
    num_judgment = Random_num(418,10)
    num_completion = Random_num(190,10)
    #将生成的编号存入字典`question_num`
    question_num = {"单选题号":num_single_choice,
           "多选题号":num_mult_choice,
            "判断题号":num_judgment,
            "填空题号":num_completion
           }
    #根据随机生成的题目编号去题库选题,并存入`questions_data`
    questions_data = {
        "单选题":Question("单选题",question_num["单选题号"]),
        "多选题":Question("多选题",question_num["多选题号"]),
        "判断题":Question("判断题",question_num["判断题号"]),
        "填空题":Question("填空题",question_num["填空题号"])
    }
    #将试题写入word文档,并保存
    To_word(number,questions_data)
    #将试题答案写入word文档,并保存
    Answer(number,questions_data)
    print(f"试卷{number}及答案完成!")

获取人人电影网的链接

uupython阅读(311)

这段代码是一个网络爬虫,用于从指定网站中获取电影、电视剧等资源的链接,并将链接保存为JSON文件。以下是代码的解释和完整版本:

需要注意以下几点:

  1. 代码中使用了requests库来发送HTTP请求,并使用BeautifulSoup库来解析HTML内容。这些库在网页内容处理中非常常用。
  2. USER_AGENTS列表存储了不同的User Agent字符串,用于模拟不同的浏览器请求。
  3. get_target_links函数用于从网站中获取目标链接,save_to_json函数用于将链接保存为JSON文件。
  4. 主程序中,用户可以选择不同的类别来获取相应的链接。同时,程序会保存断点信息,以便在下次运行时继续。
  5. 代码中加入了随机暂停,以模拟人为浏览行为,以避免对服务器造成过大压力。
  6. 请确保你的爬取行为是合法合规的,并且遵循网站的使用规则。使用时请注意适当修改相关参数以满足你的需求。
import requests
import random
import time
import json
import logging
import os
from bs4 import BeautifulSoup
from datetime import datetime
 
USER_AGENTS = [
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3 Edge/16.16299",
    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36",
    # 可根据需要添加更多的User Agent
]
 
# 设置日志模块使用utf-8编码
log_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'crawler.log')
logging.basicConfig(filename=log_file_path, level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', encoding='utf-8')
 
def get_target_links(category, listnum, start_page=1):
    category_list = [
        ("dongman", "13"),
        ("movie", "2"),
        ("dianshiju", "6"),
        ("zongyi", "10"),
    ]
 
    # Check if the provided category and listnum are within valid range
    if not (1 <= category <= len(category_list)):
        logging.warning("无效的选项。")
        return []
 
    if not (1 <= listnum <= len(category_list)):
        logging.warning("无效的选择。")
        return []
 
    # Get the selected category and listnum
    selected_category, selected_listnum = category_list[category - 1]
 
    base_url = f"https://www.rrdynb.com/{selected_category}/list_{selected_listnum}_"
    page_number = start_page
    target_links = []
 
    while True:
        url = f"{base_url}{page_number}.html"
 
        # 随机选择一个User Agent
        user_agent = random.choice(USER_AGENTS)
        headers = {"User-Agent": user_agent}
 
        logging.info(f"正在获取第{page_number}页...")
        response = requests.get(url, headers=headers)
 
        if response.status_code == 200:
            logging.info(f"成功连接,正在抓取...")
            soup = BeautifulSoup(response.text, "html.parser")
            target_link = soup.find("a", class_="movie-thumbnails")["href"]
            target_links.append(target_link)
            page_number += 1
            time.sleep(3)  # 暂停
        elif response.status_code == 404:
            logging.info(f"已获取所有页面,共{page_number - 1}页。")
            break
        else:
            logging.warning(f"获取第{page_number}页失败。5秒后重试...")
            time.sleep(5)  # 5秒后重试
            continue
 
    return target_links
 
def save_to_json(target_links, category):
    categories = {
        1: "动漫",
        2: "电影",
        3: "电视剧",
        4: "老电影",
    }
 
    if category not in categories:
        logging.warning("无效的选项。")
        return
 
    today = datetime.now().strftime("%Y%m%d")
    file_name = f"{categories[category]}_{today}.json"
    file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), file_name)
    with open(file_path, "w", encoding="utf-8") as json_file:
        json.dump(target_links, json_file, ensure_ascii=False, indent=4)
 
if __name__ == "__main__":
    print("1. 动漫")
    print("2. 电影")
    print("3. 电视剧")
    print("4. 老电影")
 
    selected_category = int(input("请选择一个选项:"))
 
    logging.info(f"用户选择了选项 {selected_category}")
 
    checkpoint_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'checkpoint.txt')
    try:
        with open(checkpoint_file_path, 'r') as checkpoint_file:
            start_page = int(checkpoint_file.readline())
    except FileNotFoundError:
        start_page = 1
 
    target_links = get_target_links(selected_category, start_page)
    if target_links:
        today = datetime.now().strftime("%Y%m%d")
        with open(checkpoint_file_path, 'w') as checkpoint_file:
            checkpoint_file.write(f"{len(target_links) + start_page - 1}_{today}")
            logging.info("断点已保存。")
 
        logging.info("目标链接列表:")
        for link in target_links:
            logging.info(link)
 
            # 保存当前页的链接至JSON文件并添加https://www.rrdynb.com前缀
            current_page_links = [f"https://www.rrdynb.com{link}" for link in target_links]
            save_to_json(current_page_links, selected_category)
            logging.info("当前页链接已保存至对应的JSON文件。")
 
        logging.info(f"{len(target_links)}页获取成功已保存到JSON文件当中。")
    else:
        logging.warning("未找到目标链接。")

一键提取局域网在线IP和对应Mac地址 

uupython阅读(303)

这段代码用于获取局域网内在线设备的IP地址和MAC地址,并分别将其存储在IP列表和MAC列表中。通过调用get_ip()函数可以获取IP列表,通过调用get_mac()函数可以获取MAC列表。以下是代码的解释和完整版本:

import os
import re

# 获取局域网在线IP和对应Mac地址
def get_ip_mac():
    ip_mac = os.popen('arp -a')
    ip_mac = ip_mac.read()
    ip_mac = ip_mac.split('\n')
    ip_mac = ip_mac[3:-1]
    ip_mac_list = []
    for i in ip_mac:
        ip_mac_dict = {'ip': re.findall(r'(\d+\.){3}\d+', i)[0],
                       'mac': re.findall(r'(([0-9a-fA-F]{2}-){5}[0-9a-fA-F]{2})', i)[0][0]}
        ip_mac_list.append(ip_mac_dict)
    return ip_mac_list

# 获取局域网在线IP
def get_ip():
    ip_mac_list = get_ip_mac()
    ip_list = []
    for i in ip_mac_list:
        ip_list.append(i['ip'])
    return ip_list

# 获取局域网在线Mac地址
def get_mac():
    ip_mac_list = get_ip_mac()
    mac_list = []
    for i in ip_mac_list:
        mac_list.append(i['mac'])
    return mac_list

if __name__ == '__main__':
    print(get_ip())
    print(get_mac())

解释和注意事项:

  1. 代码中使用了os.popen('arp -a')来执行arp -a命令,该命令用于查看局域网内的ARP缓存表,从而获取局域网中在线设备的IP和MAC地址。这种方法仅适用于Windows系统。
  2. 正则表达式用于从命令输出中提取IP和MAC地址。re.findall(r'(\d+\.){3}\d+', i)[0]用于提取IP地址,re.findall(r'(([0-9a-fA-F]{2}-){5}[0-9a-fA-F]{2})', i)[0][0]用于提取MAC地址。
  3. ip_mac_list列表存储每个设备的IP和MAC信息,最终get_ip()get_mac()函数分别提取IP列表和MAC列表。
  4. 代码适用于获取局域网中在线设备的IP和MAC信息,但请注意在实际使用时,一些设备可能因为安全策略或网络配置的不同,可能无法被正常获取。
  5. 请确保你的使用行为是合法合规的,并且仅在你有权限和合法目的下使用该代码。