[英]Python - Retrieve text from inside copied URL
I am trying to make it so when you copy a url to your clipboard it looks inside of that link for the raw text and then copies that raw text to your clipboard automatically.我试图做到这一点,当您将 url 复制到剪贴板时,它会在原始文本的链接内查找,然后自动将该原始文本复制到剪贴板。
I also need it to change this link from https://pastebin.com/vwq1D1NA to https://pastebin.com/raw/vwq1D1NA before it extracts the text from inside.在从内部提取文本之前,我还需要它将此链接从https://pastebin.com/vwq1D1NA更改为https://pastebin.com/raw/vwq1D1NA 。
For example from this pastebin link I want to automatically extract & copy the text "this is the text I want to copy to my clipboard" to my clipboard.例如,从这个 pastebin 链接中,我想自动提取并将文本“这是我想复制到剪贴板的文本”复制到剪贴板。
Copy link > Searches for text inside link > Copies text inside link to clipboard复制链接 > 搜索链接内的文本 > 将链接内的文本复制到剪贴板
import requests
import pyperclip
url = 'https://pastebin.com/raw/'
r = requests.get(url)
content = r.text
pyperclip.copy(content)
pyperclip.paste()
print(content)
UPDATE:更新:
If you'd like to only change the URL to the raw content URL on Pastebin when you copy it.如果您只想在复制时将 URL 更改为 Pastebin 上的原始内容 URL。 Try this:尝试这个:
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)
Try this.尝试这个。 Explanation in code comment.代码注释中的说明。
My understanding of the requirements of your project:我对你项目要求的理解:
If this is not correct, please let us know.如果这不正确,请告诉我们。
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.