Python-从内部复制的URL检索文本

Python-从内部复制的URL检索文本,python,python-requests,Python,Python Requests,我正在尝试这样做,当你将url复制到剪贴板时,它会在链接中查找原始文本,然后自动将原始文本复制到剪贴板 我还需要它将此链接从更改为,然后再从内部提取文本 例如,从这个pastebin链接,我想自动将文本“这是我想复制到剪贴板的文本”提取并复制到我的剪贴板 复制链接>搜索链接内的文本>将链接内的文本复制到剪贴板 import requests import pyperclip url = 'https://pastebin.com/raw/' r = requests.get(url)

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

我还需要它将此链接从更改为,然后再从内部提取文本

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

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

import requests
import pyperclip

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

r = requests.get(url) 

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

更新:

如果复制时只想将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)
试试这个。代码注释中的解释

我对贵项目要求的理解:

  • 检查剪贴板中是否有Pastebin链接
  • 获取链接的文本内容
  • 将内容复制到剪贴板
  • 如果这不正确,请让我们知道

    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)
    

    每次都会是pastebin链接吗?pastebin&hastebin。两者都使用/raw/。我遇到的问题只是从用户那里获取URL,然后将其更改为/raw。输入到代码中的链接也能满足我的所有要求。你是如何从用户那里获取url的?url将来自用户的剪贴板,在他们复制了一个链接后,不幸的是,链接不起作用,我只是试图接收“/raw/linkhere/text从链接内部复制,然后将其复制到我的剪贴板。即使被复制,链接也保持不变。@tjm111245那么,当您从Pastebin复制链接时,是否希望Python将链接更改为原始文本链接?