beiklive's blog beiklive's blog
首页
  • 语言学习

    • C/C++
    • Python
    • Qt
  • 系统&引擎

    • Linux
    • Godot
  • 啥都学

    • 夏姬八学
    • 好好学习
  • 折腾记录

    • 树莓派
    • Obsidian
    • 实践记录
  • 技术文档
  • 工具网站
  • Github项目
  • 友情链接
  • 关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

beiklive

全沾艺人
首页
  • 语言学习

    • C/C++
    • Python
    • Qt
  • 系统&引擎

    • Linux
    • Godot
  • 啥都学

    • 夏姬八学
    • 好好学习
  • 折腾记录

    • 树莓派
    • Obsidian
    • 实践记录
  • 技术文档
  • 工具网站
  • Github项目
  • 友情链接
  • 关于
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 配置和换源
  • 代码片段
    • selenium使用
    • log日志
    • tornado
    • 解析yaml
    • 监控文件目录变化
    • 图片处理
    • 视频优化
    • PDF 转图片
    • 下载器
    • 获取 Github 最新 hosts
  • 打包py为可执行文件
  • Note_Python
beiklive
2024-05-07
目录

代码片段

obsidian link

01.配置和换源

# selenium使用

安装:

pip install selenium
1

相关代码

from selenium import webdriver
from selenium.webdriver.common.by import By

# chrome_driver=r"C:\Python310\Lib\site-packages\selenium\webdriver\chrome\chromedriver.exe"
# driver=webdriver.Chrome(executable_path=chrome_driver)
driver=webdriver.Chrome()
driver.get(url)   #获取当前url   driver.current_url
# 查找元素
userName_tag = driver.find_element(By.ID, 'tbLoginName')
res = driver.find_element(By.XPATH, "//li[text()='导出为']")
# 输入内容
userName_tag.send_keys('100019050')
# 点击
res.click()
1
2
3
4
5
6
7
8
9
10
11
12
13
14

判断元素存在

from selenium.common.exceptions import NoSuchElementException
# 元素是否存在 is_element_present(driver, By.XPATH, "//div[text()='回收站']")
def is_element_present(driver, type, value):
    try:
        id = driver.find_element(type, value)
    except NoSuchElementException as e:
        return False
    return True
1
2
3
4
5
6
7
8

双击动作

from  selenium.webdriver.common.action_chains import ActionChains
ActionChains(driver).double_click(driver.find_element_by_name(“name”)).perform()
1
2

指定缓存目录,保留登陆状态

# 目录路径在:chrome://version/   个人资料路径
option = webdriver.ChromeOptions()
option.add_argument('--user-data-dir=/home/dongjie/.config/google-chrome')
driver = webdriver.Chrome(options = option)
1
2
3
4

# log日志

import logging

logging.debug("This is a debug log.")
logging.info("This is a info log.")
logging.warning("This is a warning log.")
logging.error("This is a error log.")
logging.critical("This is a critical log.")
1
2
3
4
5
6
7

日志级别

logging.basicConfig(level=logging.DEBUG)  #默认只显示warning及以上
1

日志格式

LOG_FORMAT = "%(asctime)s - %(levelname)s - %(message)s"
logging.basicConfig(format=LOG_FORMAT)
#2017-05-08 14:29:53,783 - DEBUG - This is a debug log.
#2017-05-08 14:29:53,784 - INFO - This is a info log.
#2017-05-08 14:29:53,784 - WARNING - This is a warning log.
#2017-05-08 14:29:53,784 - ERROR - This is a error log.
#2017-05-08 14:29:53,784 - CRITICAL - This is a critical log.
1
2
3
4
5
6
7

日期格式

DATE_FORMAT = "%m/%d/%Y %H:%M:%S %p"
logging.basicConfig(datefmt=DATE_FORMAT)
1
2

输出到文件

logging.basicConfig(filename='my.log')
1

# tornado

命令行参数

from tornado.options import define, options

define("port", default=8011, help="运行端口", type=int)
define("uarg", default='/gitlab-push', help="网页路由", type=str)
define("script", default='./gitlab-hook.sh', help="hook脚本", type=str)
define("isExecute", default='true', help="控制是否执行脚本", type=str)

print(options.port)
1
2
3
4
5
6
7
8

使用

python app.py --port=3000 --script="myscript.sh" --uarg="/post" --isExecute="false"
1

# 解析yaml

test.yml如下

# 用户名
user_name: tinker

# 日期
date: 2022-02-21

# user_name_list
user_name_list:
 - user_name: Tom
 - user_name: Jack
 - user_name: tinker
1
2
3
4
5
6
7
8
9
10
11

解析代码如下

#!/usr/bin/python
# vim: set fileencoding:utf-8
import os

import yaml

# 获取yaml文件路径
yamlPath = os.path.join("D:\\test\\", "config.yml")

# open方法打开直接读出来
f = open(yamlPath, 'r', encoding='utf-8')
cfg = f.read()

params = yaml.load(cfg, Loader=yaml.SafeLoader)

user_name = params['user_name']
plan_date = params['date'] if params['date'] is not None else ''
user_name_list = params['user_name_list']

print(user_name)
print(plan_date)

for element in user_name_list:
    print(element.get('user_name'))
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# 监控文件目录变化

安装

pip3 install watchdog
1

代码

import sys
import time
import logging

from watchdog.observers import Observer
from watchdog.events import *


# 处理器
class FileEventHandler(FileSystemEventHandler):
    def __init__(self):
        FileSystemEventHandler.__init__(self)

    def on_created(self, event):
        if event.is_directory:
            pass
        else:
            print("file created:{0}".format(event.src_path))

if __name__=='__main__':
    logging.basicConfig(level=logging.INFO,
                        format='%(asctime)s - %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S')
    path = sys.argv[1] if len(sys.argv) > 1 else '.'
    event_handler = FileEventHandler()
    observer = Observer()
    observer.schedule(event_handler,'./test', recursive=True)
    observer.start()
    try:
        while True:
            time.sleep(1)
    except KeyboardInterrupt:
        observer.stop()
    observer.join()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

输出

file created:./test/1/q.txt
1

# 图片处理

使用流行的是 Pillow 模块,可以在下面找到优化图像所需的大部分方法。

# Image Optimizing
# pip install Pillow
import PIL
# Croping 
im = PIL.Image.open("Image1.jpg")
im = im.crop((34, 23, 100, 100))
# Resizing
im = PIL.Image.open("Image1.jpg")
im = im.resize((50, 50))
# Flipping
im = PIL.Image.open("Image1.jpg")
im = im.transpose(PIL.Image.FLIP_LEFT_RIGHT)
# Rotating
im = PIL.Image.open("Image1.jpg")
im = im.rotate(360)
# Compressing
im = PIL.Image.open("Image1.jpg")
im.save("Image1.jpg", optimize=True, quality=90)
# Bluring
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.BLUR)
# Sharpening
im = PIL.Image.open("Image1.jpg")
im = im.filter(PIL.ImageFilter.SHARPEN)
# Set Brightness
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Brightness(im)
im = im.enhance(1.5)
# Set Contrast
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageEnhance.Contrast(im)
im = im.enhance(1.5)
# Adding Filters
im = PIL.Image.open("Image1.jpg")
im = PIL.ImageOps.grayscale(im)
im = PIL.ImageOps.invert(im)
im = PIL.ImageOps.posterize(im, 4)
# Saving
im.save("Image1.jpg")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

# 视频优化

使用 Moviepy 模块,允许你修剪、添加音频、设置视频速度、添加 VFX 等等。

# Video Optimizer
# pip install moviepy
import moviepy.editor as pyedit
# Load the Video
video = pyedit.VideoFileClip("vid.mp4")
# Trimming
vid1 = video.subclip(0, 10)
vid2 = video.subclip(20, 40)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# Speed up the video
final_vid = final_vid.speedx(2)
# Adding Audio to the video
aud = pyedit.AudioFileClip("bg.mp3")
final_vid = final_vid.set_audio(aud)
# Reverse the Video
final_vid = final_vid.fx(pyedit.vfx.time_mirror)
# Merge two videos
vid1 = pyedit.VideoFileClip("vid1.mp4")
vid2 = pyedit.VideoFileClip("vid2.mp4")
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# Add VFX to Video
vid1 = final_vid.fx(pyedit.vfx.mirror_x)
vid2 = final_vid.fx(pyedit.vfx.invert_colors)
final_vid = pyedit.concatenate_videoclips([vid1, vid2])
# Add Images to Video
img1 = pyedit.ImageClip("img1.jpg")
img2 = pyedit.ImageClip("img2.jpg")
final_vid = pyedit.concatenate_videoclips([img1, img2])
# Save the video
final_vid.write_videofile("final.mp4")
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

# PDF 转图片

使用流行的 PyMuPDF 模块,该模块以其 PDF 文本提取而闻名。

# PDF to Images
# pip install PyMuPDF
import fitz
def pdf_to_images(pdf_file):
    doc = fitz.open(pdf_file)
    for p in doc:
        pix = p.get_pixmap()
        output = f"page{p.number}.png"
        pix.writePNG(output)
pdf_to_images("test.pdf")
1
2
3
4
5
6
7
8
9
10

# 下载器

# Python Downloader
# pip install internetdownloadmanager
import internetdownloadmanager as idm
def Downloader(url, output):
    pydownloader = idm.Downloader(worker=20,
                                part_size=1024*1024*10,
                                resumable=True,)
 
    pydownloader .download(url, output)
Downloader("Link url", "image.jpg")
Downloader("Link url", "video.mp4")
1
2
3
4
5
6
7
8
9
10
11

# 获取 Github 最新 hosts

该脚本会把获取到的 hosts 信息添加到 target_file 指定的文件中, 文件中需要添加 start_marker 和 end_marker 否则无法插入, 结合 crontab 设置好定时任务就可以实现自动获取最新的github的host并更新到 系统的hosts中

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import requests

# 定义源URL和目标文件
source_url = "https://gitlab.com/ineo6/hosts/-/raw/master/hosts"
target_file = "host.txt"

# 定义标记
start_marker = "# >>>>start\n"
end_marker = "\n\n# <<<<end\n"

def fetch_latest_content(url):
    # 发送GET请求获取最新内容
    response = requests.get(url)
    if response.status_code == 200:
        return response.text.splitlines()
    else:
        print(f"无法从 {url} 获取内容。")
        return None

def update_host_file_with_new_content(source_content, file_path):
    # 构建新的内容
    new_content = start_marker + '\n' + '\n'.join(source_content) + end_marker + '\n'

    # 替换目标文件中的内容
    try:
        with open(file_path, 'r+') as f:
            file_data = f.read()
            start_pos = file_data.find(start_marker)
            end_pos = file_data.find(end_marker, start_pos + len(start_marker))

            if start_pos != -1 and end_pos != -1:
                f.seek(0)
                f.truncate()
                f.write(file_data[:start_pos])
                f.write(new_content)
                f.write(file_data[end_pos + len(end_marker):])
                print("目标文件已更新。")
            else:
                print("未找到标记,无法替换内容。")

    except FileNotFoundError:
        print(f"文件 {file_path} 未找到。")
    except IOError:
        print(f"无法写入到文件 {file_path}。")

def main():
    # 获取最新内容
    latest_content = fetch_latest_content(source_url)
    if latest_content:
        # 更新目标文件中的内容
        update_host_file_with_new_content(latest_content, target_file)
    else:
        print("未能获取最新内容。")

if __name__ == "__main__":
    main()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
编辑 (opens new window)
#python
上次更新: 2024/05/22, 14:11:38
配置和换源
打包py为可执行文件

← 配置和换源 打包py为可执行文件→

最近更新
01
爬虫技术与法律风险:个人开发者的注意事项
05-22
02
个人开发者的抉择:个人工作室 vs 公司主体 🤔
05-22
03
《计算机网络,自顶向下方法》笔记(一)
05-20
更多文章>
Theme by Vdoing | Copyright © 2024-2024 beiklive | 苏ICP备20038092号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式