python常用小程序

python常用小程序原标题:python常用小程序

导读:

相信很多小伙伴在学习Python的过程中,都会编写一些实用的小程序来提高自己的编程技能,就让我来给大家分享几个超实用的Python常用小程序,让你在编程路上更加得心应手!斗图神...

相信很多小伙伴在学习Python的过程中,都会编写一些实用的小程序来提高自己的编程技能,就让我来给大家分享几个超实用的Python常用小程序,让你在编程路上更加得心应手!

python常用小程序

斗图神器

相信大家在聊天时都喜欢用表情包来表达自己的情感,那么如何用Python实现一个斗图神器呢?我们需要使用requests库来爬取表情包图片,然后将图片保存到本地,具体代码如下:

import requests
from bs4 import BeautifulSoup
def download_images(url, num):
    headers = {'User-Agent': 'Mozilla/5.0'}
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    img_tags = soup.find_all('img', limit=num)
    for img_tag in img_tags:
        img_url = img_tag['src']
        if not img_url.startswith('http'):
            img_url = 'http:' + img_url
        img_name = img_url.split('/')[-1]
        with open(img_name, 'wb') as f:
            f.write(requests.get(img_url, headers=headers).content)
        print(f'下载成功:{img_name}')
# 使用示例
download_images('https://www.doutula.com/photo/list', 10)

天气查询

想知道自己所在城市的天气状况?用Python轻松实现!我们可以使用requests库和BeautifulSoup库来爬取天气信息,以下是一个简单的天气查询小程序:

import requests
from bs4 import BeautifulSoup
def get_weather(city):
    url = f'http://www.weather.com.cn/weather/{city}.shtml'
    headers = {'User-Agent': 'Mozilla/5.0'}
    response = requests.get(url, headers=headers)
    soup = BeautifulSoup(response.text, 'html.parser')
    today_weather = soup.find('div', class_='today')
    title = today_weather.find('h1').text
    weather_info = today_weather.find_all('p')
    print(f'城市:{title}')
    for info in weather_info:
        print(info.text)
# 使用示例
get_weather('101010100')

文件压缩与解压缩

有时候我们需要对文件进行压缩或解压缩,Python提供了zipfile库来实现这一功能,以下是一个简单的文件压缩与解压缩小程序:

import zipfile
def compress_files(zip_name, file_list):
    with zipfile.ZipFile(zip_name, 'w') as z:
        for file in file_list:
            z.write(file)
    print(f'压缩成功:{zip_name}')
def decompress_files(zip_name, extract_path):
    with zipfile.ZipFile(zip_name, 'r') as z:
        z.extractall(extract_path)
    print(f'解压缩成功:{extract_path}')
# 使用示例
compress_files('example.zip', ['file1.txt', 'file2.txt'])
decompress_files('example.zip', 'extract')

自动发送邮件

有时候我们需要定时发送邮件,Python的**tplib库和email库可以帮助我们实现这一功能,以下是一个简单的自动发送邮件小程序:

import **tplib
from email.mime.text import MIMEText
from email.header import Header
def send_email(sender, password, receiver, subject, body):
    **tp_server = '**tp.example.com'
    message = MIMEText(body, 'plain', 'utf-8')
    message['From'] = Header(sender, 'utf-8')
    message['To'] = Header(receiver, 'utf-8')
    message['Subject'] = Header(subject, 'utf-8')
    try:
        **tp_obj = **tplib.**TP()
        **tp_obj.connect(**tp_server, 25)
        **tp_obj.login(sender, password)
        **tp_obj.sendmail(sender, [receiver], message.as_string())
        print('邮件发送成功')
    except **tplib.**TPException as e:
        print(f'邮件发送失败:{e}')
# 使用示例
send_email('sender@example.com', 'password', 'receiver@example.com', 'Test', 'This is a test email.')

就是今天分享的四个Python常用小程序,希望对大家有所帮助!Python还有很多其他实用的功能等待我们去探索,只要我们用心去学习,一定能编写出更多有趣、实用的小程序,让我们一起加油吧!

返回列表
上一篇:
下一篇: