跳到正文

个人记录

Python获取云剪切板内容

YuTianQwQ约 6 分钟
原文
文章信息与版本

文章信息

发布
2023年6月12日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 6 分钟

版本

  1. v1当前

这玩意就显示了头像,用户名及颜色,云剪切板内容

要安装这些库:

代码
pip install selenium beautifulsoup4 requests Pillow

代码:

Python
from selenium import webdriver
from bs4 import BeautifulSoup
import requests
import os
import re
import urllib
import json
import tkinter as tk
from PIL import Image, ImageTk
from tkinter import font as tkFont
name = ""
col = ""
def parse_rgb_string(rgb_string):
    parts = rgb_string.replace("rgb(", "").replace(")", "").split(",")
    return tuple(int(part.strip()) for part in parts)
def rgb_to_hex(rgb):
    return '#%02x%02x%02x' % rgb
def paste_get(input_url):
    driver = webdriver.Firefox()
    url = input_url
    driver.get(url)
    html = driver.page_source
    soup = BeautifulSoup(html, 'html.parser')
    span_elements = soup.find_all('span', style=re.compile(r"font-weight: bold; color: rgb"))
    img_elements = soup.find_all('img', class_="avatar")
    for i, img in enumerate(img_elements):
        img_url = img['src']
        response = requests.get(img_url, stream=True)
        if response.status_code == 200:
            filename = os.path.basename(img_url)
            if filename == "1.png":
                continue
            with open("head.png", 'wb') as out_file:
                out_file.write(response.content)
        else:
            pass
    
    for span in span_elements:
        color_match = re.search(r"color: (rgb\(\d{1,3}, \d{1,3}, \d{1,3}\))", span['style'])
        if color_match:
            color = color_match.group(1)
        else:
            color = "unknown"
        name = span.text.strip()
        col = color
        break
    driver.quit()
    return name+"\n"+col
    
def text_get(url):
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36"
    }
    
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        soup = BeautifulSoup(response.text, 'html.parser')
        script_tag = soup.find('script', string=re.compile('window._feInjection'))
        json_str = script_tag.string.split('decodeURIComponent("')[1].split('"))')[0]
        decoded_json_str = urllib.parse.unquote(json_str)
        data = json.loads(decoded_json_str)
        encoded_content = data['currentData']['paste']['data']
        decoded_content = urllib.parse.unquote(encoded_content)
        return decoded_content
    else:
        print("请求失败,状态码:", response.status_code)
url = "https://www.luogu.com.cn/paste/z0lz7yr1"
tmp = paste_get(url)
n, col = tmp.split("\n")
print(n)
print(col)
text = text_get(url)
image_path = "head.png"
username = n
color = parse_rgb_string(col)
color = rgb_to_hex(color)
text = text
root = tk.Tk()
root.geometry("600x800")
root.title("云剪切板获取")
image = Image.open(image_path)
photo = ImageTk.PhotoImage(image)
label_image = tk.Label(root, image=photo)
label_image.pack()
username_font = tkFont.Font(family="Helvetica", size=16, weight="bold")
label_username = tk.Label(root, text=username, fg=color, font=username_font)
label_username.pack()
content_font = tkFont.Font(family="Helvetica", size=14)
label_content = tk.Label(root, text=text, font=content_font, wraplength=400)
label_content.pack()
root.mainloop()
原文