如何在 chrome Python 中保存当前打开选项卡的 url

问题描述 投票:0回答:1

Python 脚本,用于保存网络浏览器中当前打开的选项卡。 用于保存网络浏览器中当前打开的选项卡的 Python 脚本。

我已经成功为 Firefox 实现了它,现在我希望将其扩展以支持 Chrome。

python google-chrome url firefox
1个回答
0
投票

试试这个代码

更近.py:

import pyautogui
from tkinter import Tk
from time import sleep
  
  SLEEP_TIME = 0.5
  MAX_DUPLICATE_COUNT = 5
  links = []
  filename = "Trial01.txt"
 
 
 def readURLs():
    pyautogui.hotkey('win', '4')      # Open Google Chrome
    sleep(SLEEP_TIME)
    # pyautogui.position()              # To read current mouse position
    # pyautogui.moveTo(298, 69)
    pyautogui.hotkey('ctrl', '1')     # Open 1st Tab
    duplicate_count = 0
    while duplicate_count < MAX_DUPLICATE_COUNT:
       pyautogui.click(x=1000, y=70, button='left')      # Click on URL Bar
       sleep(SLEEP_TIME)
       pyautogui.hotkey('ctrl', 'a')                 # Select complete URL
       sleep(SLEEP_TIME)
       pyautogui.hotkey('ctrl', 'c')                 # Copy URL
       sleep(SLEEP_TIME)
       link = Tk().clipboard_get()                      # Read from Clipboard
       if len(links) > 0 and link in links:            # Check if the URL has already been saved
          duplicate_count += 1
          # print('Duplicate ' + str(duplicate_count) + ' found: ' + link)
       else:
          links.append(link)                      # Else append the URL to list
       print(link)
       sleep(SLEEP_TIME)
       pyautogui.hotkey('ctrl', 'tab')                   # Switch to next tab
       sleep(SLEEP_TIME)
    
 def saveToFile():
    file = open(filename, 'w')
    file.write('\n'.join(links  file.close()
 
 readURLs()
 saveToFile()
 pyautogui.hotkey('win', 'd')                        # Close all windows: indicates end of script

开场白:

import os
  
fileName = "Trial01.txt"
  
commandString = 'start chrome /new-window "'
file = open(fileName)
urls = file.readlines()
urls = [url.strip() for url in urls]
commandString += '" "'.join(urls) + '"'
print(commandString)
# Execute command in Command Prompt to start new Chrome window and open the URLs
os.system(commandString)
© www.soinside.com 2019 - 2024. All rights reserved.