Advertisement
smeech

Extract text and URL from copied web-page link

Jan 26th, 2025
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
YAML 1.52 KB | None | 0 0
  1.  - trigger: :test
  2.     replace: '{{Clipb}} {{Output}}'  
  3.     vars:
  4.       - name: Clipb
  5.         type: clipboard
  6.       - name: Output
  7.         type: script
  8.         params:
  9.           args:
  10.            - python
  11.             - -c
  12.             - |
  13.              import sys
  14.               from PyQt5.QtWidgets import QApplication
  15.               from bs4 import BeautifulSoup
  16.  
  17.               def extract_urls_from_html(html_content):
  18.                   soup = BeautifulSoup(html_content, 'html.parser')
  19.                   anchors = soup.find_all('a')
  20.                   urls = [a['href'] for a in anchors if a.get('href')]
  21.                   return urls
  22.  
  23.               def main():
  24.                   app = QApplication(sys.argv)
  25.  
  26.                   clipboard = app.clipboard()
  27.                   mime_data = clipboard.mimeData()
  28.                  
  29.                   if mime_data.hasHtml():
  30.                       html_content = mime_data.html()
  31.                       urls = extract_urls_from_html(html_content)
  32.                      
  33.                       if urls:
  34.                           for url in urls:
  35.                               print(url)
  36.                       else:
  37.                           print("No URLs found in clipboard content.")
  38.                   else:
  39.                       print("Clipboard does not contain HTML content.")
  40.  
  41.                   # You don't need to run the main loop of the QApplication, so we can exit here.
  42.                   sys.exit()
  43.  
  44.               if __name__ == "__main__":
  45.                   main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement