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

时间:2020-03-03 22:44:15

标签: python python-requests

我正在尝试做到这一点,因此当您将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) 

1 个答案:

答案 0 :(得分:0)

更新:

如果您只想在复制时将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)