Skip to content

Instantly share code, notes, and snippets.

@smeech
Created January 31, 2025 16:54
Show Gist options
  • Save smeech/e5182ac9b1447bfbb3da5eb4c251447e to your computer and use it in GitHub Desktop.
Save smeech/e5182ac9b1447bfbb3da5eb4c251447e to your computer and use it in GitHub Desktop.
[Text URL extract] Extract text and URL from copied web-page link. #espanso #python
- trigger: :test
replace: '{{Clipb}} {{Output}}'
vars:
- name: Clipb
type: clipboard
- name: Output
type: script
params:
args:
- python
- -c
- |
import sys
from PyQt5.QtWidgets import QApplication
from bs4 import BeautifulSoup
def extract_urls_from_html(html_content):
soup = BeautifulSoup(html_content, 'html.parser')
anchors = soup.find_all('a')
urls = [a['href'] for a in anchors if a.get('href')]
return urls
def main():
app = QApplication(sys.argv)
clipboard = app.clipboard()
mime_data = clipboard.mimeData()
if mime_data.hasHtml():
html_content = mime_data.html()
urls = extract_urls_from_html(html_content)
if urls:
for url in urls:
print(url)
else:
print("No URLs found in clipboard content.")
else:
print("Clipboard does not contain HTML content.")
# You don't need to run the main loop of the QApplication, so we can exit here.
sys.exit()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment