有时候我们需要压缩图片以节省空间,可以使用Python的PIL
库。
from PIL import Image
def compress\_image(input_file, output_file, quality):
img = Image.open(input_file)
img.save(output_file, quality=quality)
input_file = "example.jpg"
output_file = "compressed.jpg"
quality = 80 # 质量,1-100之间,数值越低压缩率越高
compress_image(input_file, output_file, quality)
4. 网络爬虫
网络爬虫可以帮助你快速获取网页上的信息。使用Python的requests
和BeautifulSoup
库,你可以轻松实现。
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# 提取网页上的信息,例如提取所有的链接
links = [a.get("href") for a in soup.find_all("a")]
5. 发送邮件
使用Python的smtplib
库可以轻松实现发送邮件的功能。
import smtplib
def send\_email(subject, body, to_email, from_email, password):
message = f"Subject: {subject}\n\n{body}"
with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server:
server.login(from_email, password)
server.sendmail(from_email, to_email, message)
subject = "Hello"
body = "This is a test email."
to_email = "recipient@example.com"
from_email = "your\_email@example.com"
password = "your\_password"
send_email(subject, body, to_email, from_email, password)
6. Excel操作
使用Python的openpyxl
库可以轻松读取和修改Excel文件。
import openpyxl
# 读取Excel文件
workbook = openpyxl.load_workbook("example.xlsx")
sheet = workbook.active
# 获取单元格的值
cell_value = sheet["A1"].value
# 修改单元格的值
sheet["A1"].value = "New Value"
# 保存文件
workbook.save("output.xlsx")
7. Word操作
使用Python的python-docx
库可以轻松读取和修改Word文档。
import docx
# 读取Word文件
doc = docx.Document("example.docx")
# 获取段落文字
paragraphs = [para.text for para in doc.paragraphs]
# 添加段落
doc.add_paragraph("New paragraph")
# 保存文件
doc.save("output.docx")
8. PowerPoint操作
使用Python的python-pptx
库可以轻松读取和修改PowerPoint文件。
from pptx import Presentation
# 读取PPT文件
ppt = Presentation("example.pptx")
# 获取幻灯片数量和标题
slide_count = len(ppt.slides)
titles = [slide.shapes.title.text for slide in ppt.slides]
# 添加新的幻灯片
slide_layout = ppt.slide_layouts[0] # 选择布局
new_slide = ppt.slides.add_slide(slide_layout)
new_slide.shapes.title.text = "New Slide"
# 保存文件
ppt.save("output.pptx")
9. 读写CSV文件
使用Python的csv
库可以轻松读取和写入CSV文件。
import csv
# 读取CSV文件
with open("example.csv", "r") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
print(row)
# 写入CSV文件
data = [["Name", "Age"], ["Alice", 30], ["Bob", 25]]
with open("output.csv", "w") as csvfile:
writer = csv.writer(csvfile)
for row in data:
writer.writerow(row)
10. 计划任务
使用Python的schedule
库可以定时执行任务。
import schedule
import time
def job():
print("Task executed!")
# 每隔10秒执行一次任务
schedule.every(10).seconds.do(job)
while True:
schedule.run_pending()
time.sleep(1)
11. 语音识别
使用Python的speech_recognition
库可以将语音转换为文本。
import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.Microphone() as source:
print("Say something...")
audio = recognizer.listen(source)
try:
text = recognizer.recognize_google(audio)
print("You said: ", text)
except sr.UnknownValueError:
print("Google Speech Recognition could not understand audio")
except sr.RequestError as e:
print("Could not request results from Google Speech Recognition service; {0}".format(e))
12. 文本翻译
使用Python的googletrans
库可以实现文本翻译。
from googletrans import Translator
translator = Translator()
translated = translator.translate("Hello, world!", dest="zh-CN")
print(translated.text)
13. 字符串模板
使用Python的string
库可以轻松生成动态字符串。
from string import Template
template = Template("Hello, ${name}!")
message = template.substitute(name="John")
print(message)
14. 二维码生成
使用Python的qrcode
库可以生成二维码。
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data("https://example.com")
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("qrcode.png")
15. 数据可视化
使用Python的matplotlib
库可以绘制图表。
import matplotlib.pyplot as plt
x = [1, 2, 3, 4, 5]
y = [2, 4, 6, 8, 10]
plt.plot(x, y)
plt.xlabel("X-axis")
plt.ylabel("Y-axis")
plt.title("Line Plot")
plt.show()
16. JSON操作
使用Python的json
库可以轻松读写JSON文件。
import json
# 读取JSON文件
with open("example.json", "r") as file:
data = json.load(file)
# 写入JSON文件
new_data = {"name": "John", "age": 30}
with open("output.json", "w") as file:
json.dump(new_data, file)
17. 文件差异比较
使用Python的difflib
库可以比较两个文件的差异。
本人从事网路安全工作12年,曾在2个大厂工作过,安全服务、售后服务、售前、攻防比赛、安全讲师、销售经理等职位都做过,对这个行业了解比较全面。
最近遍览了各种网络安全类的文章,内容参差不齐,其中不伐有大佬倾力教学,也有各种不良机构浑水摸鱼,在收到几条私信,发现大家对一套完整的系统的网络安全从学习路线到学习资料,甚至是工具有着不小的需求。
最后,我将这部分内容融会贯通成了一套282G的网络安全资料包,所有类目条理清晰,知识点层层递进,需要的小伙伴可以点击下方小卡片领取哦!下面就开始进入正题,如何从一个萌新一步一步进入网络安全行业。
学习路线图
其中最为瞩目也是最为基础的就是网络安全学习路线图,这里我给大家分享一份打磨了3个月,已经更新到4.0版本的网络安全学习路线图。
相比起繁琐的文字,还是生动的视频教程更加适合零基础的同学们学习,这里也是整理了一份与上述学习路线一一对应的网络安全视频教程。
网络安全工具箱
当然,当你入门之后,仅仅是视频教程已经不能满足你的需求了,你肯定需要学习各种工具的使用以及大量的实战项目,这里也分享一份我自己整理的网络安全入门工具以及使用教程和实战。
项目实战
最后就是项目实战,这里带来的是SRC资料&HW资料,毕竟实战是检验真理的唯一标准嘛~
面试题
归根结底,我们的最终目的都是为了就业,所以这份结合了多位朋友的亲身经验打磨的面试题合集你绝对不能错过!
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!