Python 工具集合
Project description
自用库
一. 配置管理
from wmain.autosave import WAutoSave
# 创建自己的参数类, 假设是账号, 密码, 邮箱
class Friend(WAutoSave):
def __init__(self, name, age, address):
self.name = name
self.age = age
self.address = address
class User(WAutoSave):
def __init__(self, username, password, email):
self.username = username
self.password = password
self.email = email
self.friend = Friend("friend_name", 20, "friend_address")
# 创建实例
user = User('admin', '123456', 'admin@example.com')
# 绑定文件
user.bind_autosave("user.json")
# 第一次运行输出 friend_name, 第二次运行输出 new_friend_name
print(user.friend.name)
# bind之后的任何修改都可以改动到文件中
# 仅支持str, int, float, WAutoSave, 其他类型不会自动保存
user.username = "new_username"
user.password = "new_password"
user.email = "new_email"
user.friend.name = "new_friend_name"
二. requests中session的重新封装, 以及url的封装
from wmain.requests import WSession, WUrl
session = WSession()
session.ini.set_proxy(7890)
# 发送请求, 请求头已经随机携带
# 修改请求头
# session.ini.set_ua("mac")
# session.ini.set_ua("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")
resp = session.get("https://www.baidu.com")
print(resp.status_code)
# 直接用xpath
elements = resp.xpath('//*[@class="title-content-title"]')
str_elements = resp.xpath_str('//*[@class="title-content-title"]')
# 打印xpath结果
print(elements)
print(str_elements)
# 更加方便的cookie导入, cookie_str请自己定义
cookie_str = "test_key=test_value; test_key2=test_value2 "
session.load_cookies_str(cookie_str, "www.baidu.com")
get_cookies_url = WUrl("https://www.ip89.com/api2.php")
# 字符串键值会修改query, 数字键值会修改path
get_cookies_url["r"] = "1"
get_cookies_url["ac"] = "get-cookies"
get_cookies_url["url"] = "https://www.baidu.com"
# 发送请求, 自动携带cookie
resp = session.get(get_cookies_url)
print(get_cookies_url)
print(resp.json["data"]["cookies"])
# 保存到文件, 方便下次导入cookie
session.save_cookies_file("cookies.txt")
# session.load_cookies_file("cookies.txt")
三. 多线程的封装
from wmain.thread import WMultiThread, WLock
import time
# lock参数会自动传入, 最后一个参数必需有lock
def task(print_num, lock):
lock.acquire()
print(f"{print_num} has been printed")
lock.release()
time.sleep(1)
# 创建线程
thread = WMultiThread(8)
tasks = range(100)
thread.run(task, tasks)
# 等待所有线程结束
while thread.is_running:
time.sleep(1)
print("All tasks have been finished")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
wmain-2.0.1.tar.gz
(2.5 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
wmain-2.0.1-py3-none-any.whl
(2.8 kB
view details)
File details
Details for the file wmain-2.0.1.tar.gz.
File metadata
- Download URL: wmain-2.0.1.tar.gz
- Upload date:
- Size: 2.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.0 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0cfb55cf865970d49fd2b48be8434177680fc9286dfd18598c60e1d55f400460
|
|
| MD5 |
070c477bc6f47625aa5d275ce4dab25d
|
|
| BLAKE2b-256 |
0954d6ba0b62163a3ece480d9231efdfde543dc5b7389e53e801f4d9be2bf7a9
|
File details
Details for the file wmain-2.0.1-py3-none-any.whl.
File metadata
- Download URL: wmain-2.0.1-py3-none-any.whl
- Upload date:
- Size: 2.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.2.1 CPython/3.13.0 Windows/11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
14a16ac0d9ca6755277d38fb2211572c52465becc2ddbf20862782000a544186
|
|
| MD5 |
3324699b0366a4aa6cc38b2f64c6eeb6
|
|
| BLAKE2b-256 |
844b78a9c612653f0ead157a50cc50b7eb4ac656044e81dbaa53202199a70d8e
|