我想打开将 python 脚本附加到已打开的 google 选项卡。
基本上,我的目标是在页面上执行一些操作,然后获取源代码。
我已经尝试使用硒,就像我在另一个主题中发现的那样,如下所示:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option('debuggerAddress', 'localhost:9014')
driver = webdriver.Chrome(options=options)
# Get the source code of the current page
source_code = driver.page_source
但是当我尝试启动它时,没有任何附加内容。这条线接缝被堵住了:
options.add_experimental_option('debuggerAddress', 'localhost:9014')
也许我输入了错误的端口?我不知道。
有人有另一种方法将 python 脚本附加到打开的谷歌选项卡,或解决“错误”。
Windows:
"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9014
MacOS:
/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --remote-debugging-port=9014
Linux:
google-chrome --remote-debugging-port=9014
确保:
这是脚本:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_experimental_option('debuggerAddress', 'localhost:9014')
driver = webdriver.Chrome(options=options)
source_code = driver.page_source
print(source_code)