python脚本打包成exe文件

以脚本:爬取网站上无alt属性图片地址为例:

源码:

安装python库:

pip install requests beautifulsoup4

python代码: 

import requests
from bs4 import BeautifulSoup

def fetch_imgs_without_alt_or_empty_alt(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36"
    }
    try:
        # Send a GET request with headers
        response = requests.get(url, headers=headers, verify=False)
        response.raise_for_status()  # Raise an exception for HTTP errors

        # Parse the HTML content using BeautifulSoup
        soup = BeautifulSoup(response.text, 'html.parser')

        # Find all <img> tags without an 'alt' attribute or with an empty 'alt' value
        imgs = soup.find_all('img')
        imgs_without_alt_or_empty = [
            img for img in imgs if not img.has_attr('alt') or img.get('alt') == ''
        ]

        # Extract and print the 'src' of these <img> tags
        img_sources = [img.get('src') for img in imgs_without_alt_or_empty]
        
        print(f"发现 {len(img_sources)} 张图片没有 alt 属性:")
        for src in img_sources:
            print(src)
        
        return img_sources
    
    except requests.exceptions.RequestException as e:
        print(f"Error fetching the URL: {e}")
        return []

# Example usage
if __name__ == "__main__":
    url = input("输入地址源: ").strip()
    fetch_imgs_without_alt_or_empty_alt(url)

input("\n按任意键退出...")

安装对应打包库:

pip install pyinstaller

输入打包命令:

pyinstaller --onefile 脚本名字.py

打包完成后会在dist文件夹下出现exe文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值