Python从复制的URL中检索文本

2024-09-30 12:24:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试这样做,当你将url复制到剪贴板时,它会在链接中查找原始文本,然后自动将原始文本复制到剪贴板

我还需要它将此链接从https://pastebin.com/vwq1D1NA更改为https://pastebin.com/raw/vwq1D1NA,然后再从内部提取文本

例如,我想从这个pastebin链接中自动提取&;将文本“这是我要复制到剪贴板的文本”复制到我的剪贴板

复制链接>;在链接内搜索文本>;将链接内的文本复制到剪贴板

import requests
import pyperclip

url = 'https://pastebin.com/raw/' 

r = requests.get(url) 

content = r.text 
pyperclip.copy(content)
pyperclip.paste()
print(content) 

Tags: https文本importgtcomurlraw链接
1条回答
网友
1楼 · 发布于 2024-09-30 12:24:06

更新:

如果复制时只想将URL更改为Pastebin上的原始内容URL。试试这个:

import pyperclip
from time import sleep

# get the content of clipboard
cb=pyperclip.paste()

while True:
    # get the content of clipboard while True
    current_cb = pyperclip.paste()
    #safe word to stop program, copy this word to break while and stop program
    if current_cb == 'STOP!':
        print ("STOPPED.")
        break
    # if the current content doesn't match the old content stored in cb
    if current_cb != cb:
        # and if the link includes "https://pastebin.com/" and is 29 characters long
        if "https://pastebin.com/" in current_cb and len(current_cb) == 29:
            # set the raw url to the clipboard content while replacing url with raw url
            raw_url = current_cb.replace("https://pastebin.com/", "https://pastebin.com/raw/")
            # copy raw URL to the clipboard
            pyperclip.copy(raw_url)
            # paste the current content of the clipboard for testing
            paste = pyperclip.paste()
            print ("RAW URL: " + str(paste))
            # set the old clipboard content = the current clipboard conten
            cb = current_cb
            #sleep for a second
            sleep(1)

试试这个。代码注释中的解释

我对贵项目要求的理解:

  1. 检查剪贴板中是否有Pastebin链接
  2. 获取链接的文本内容
  3. 将内容复制到剪贴板

如果这不正确,请让我们知道

import requests
import pyperclip
from time import sleep

# get the content of clipboard
cb=pyperclip.paste()

while True:
    # get the content of clipboard while True
    current_cb = pyperclip.paste()
    #safe word to stop program, copy this word to break while and stop program
    if current_cb == 'STOP!':
        print ("STOPPED.")
        break
    # if the current content doesn't match the old content stored in cb
    if current_cb != cb:
        # and if the link includes "https://pastebin.com/raw/"
        if "https://pastebin.com/raw/" in current_cb:
            # set the url to the clipboard content
            url = current_cb
            # request the url
            r = requests.get(url)
            # store the response in content
            content = r.text
            # copy the contents of content to the clipboard
            pyperclip.copy(content)
            # paste the current content of the clipboard for testing
            paste = pyperclip.paste()
            print ("CURRENT CONTENT IN CLIPBOARD: " + str(paste))
            # set the old clipboard content = the current clipboard conten
            cb = current_cb
            #sleep for a second
            sleep(1)

相关问题 更多 >

    热门问题