python - Python - 从复制的 URL 中检索文本
问题描述
我正在尝试这样做,因此当您将 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)
解决方案
更新:
如果您只想在复制时将 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)
推荐阅读
- go - 无法在 vscode apple m1 中调试 Golang
- azure - 503 服务不可用 Azure Hosting (S1 Service Plan)
- regex - 如何在 A 列中的任何部分值和 B 列中的任何值之间找到匹配项?并提取?
- docker - 如何在旧的 Mac 上安装 ddev/docker?
- javascript - 如何对数组进行分类并形成新数组
- opengl - glReadPixels 的 OpenGL DSA 等效项
- blockchain - 合约地址交易列表 - Etherscan API
- php - 用 ACF 替换网站徽标
- r - R中的深度优先递归
- javascript - Next / React - 处理另一篇文章的链接