如何使用 Python 在我的网络浏览器中打开网站?
- 2025-03-14 08:57:00
- admin 原创
- 53
问题描述:
我想使用 Python 在本地计算机的网络浏览器(Chrome 或 Internet Explorer)中打开一个网站。
open("http://google.co.kr") # something like this
是否有一个模块可以为我做到这一点?
解决方案 1:
该webbrowser
模块看起来很有前途:https://www.youtube.com/watch?v= jU3P7qz3ZrM
import webbrowser
webbrowser.open('http://google.co.kr', new=2)
解决方案 2:
来自文档。
webbrowser 模块提供了一个高级接口,允许向用户显示基于 Web 的文档。在大多数情况下,只需从此模块调用 open() 函数即可。
您必须导入模块并使用函数。这将在浏览器中open()
打开https://nabinkhadka.com.np 。
在新标签页中打开:
import webbrowser
webbrowser.open('https://nabinkhadka.com.np', new = 2)
也来自文档。
如果 new 为 0,则尽可能在同一浏览器窗口中打开 URL。如果 new 为 1,则尽可能打开新浏览器窗口。如果 new 为 2,则尽可能打开新浏览器页面(“选项卡”)
因此,根据 new 的值,您可以在同一浏览器窗口或新选项卡中打开页面等。
您还可以指定要打开的浏览器(chrome、firebox 等)。为此使用get()函数。
解决方案 3:
根据说明,使用 open() 函数确实有效,并打开默认的 Web 浏览器 - 通常我会说:“为什么我不想使用 Firefox?!”(我的默认和最喜欢的浏览器)
import webbrowser as wb
wb.open_new_tab('http://www.google.com')
以上操作应该适用于计算机的默认浏览器。但是,如果您想在 Google Chrome 中打开,该怎么办?
正确的做法是:
import webbrowser as wb
wb.get('chrome %s').open_new_tab('http://www.google.com')
说实话,我不太确定我是否知道“chrome”和“google-chrome”之间的区别,但显然存在一些区别,因为他们在 webbrowser 文档中给出了两种不同的类型名称。
但是,这样做对我来说并没有立即奏效。每次我都会收到错误:
Traceback (most recent call last):
File "C:Python34programsa_temp_testing.py", line 3, in <module>
wb.get('google-chrome')
File "C:Python34libwebbrowser.py", line 51, in get
raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser
为了解决这个问题,我必须将 chrome.exe 的文件夹添加到系统 PATH。我的 chrome.exe 可执行文件位于:
C:Program Files (x86)GoogleChromeApplication
您应该亲自检查一下它是否在这里。
要将其添加到环境变量系统 PATH,请右键单击 Windows 图标并转到系统。系统控制面板小程序(开始 - 设置 - 控制面板 - 系统)。更改高级设置或高级选项卡,然后选择名为环境变量的按钮。
单击此处的环境变量后,将弹出另一个窗口。滚动浏览项目,选择 PATH,然后单击编辑。
进入此处后,单击“新建”以将文件夹路径添加到您的 chrome.exe 文件中。正如我上面所说,我的文件夹位于:
C:Program Files (x86)GoogleChromeApplication
单击“保存”并退出。然后确保重新启动计算机。
希望这有帮助!
解决方案 4:
实际上这取决于用途。如果你想在测试框架中使用它,我强烈推荐selenium-python。它是测试与 Web 浏览器相关的自动化的绝佳工具。
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
解决方案 5:
我认为应该
import webbrowser
webbrowser.open('http://gatedin.com')
注意:请确保您提供 http 或 https
如果你输入的是“ www. ”而不是“ http: ”,那么解释器不会打开浏览器,而是显示布尔值 OutPut TRUE。这里你导入的是webbrowser库
解决方案 6:
我遇到了这个问题。当我定义 Firefox 路径时,我的问题就解决了。
import webbrowser
urL='https://www.python.org'
mozilla_path="C:\\Program Files\\Mozilla Firefox\\firefox.exe"
webbrowser.register('firefox', None,webbrowser.BackgroundBrowser(mozilla_path))
webbrowser.get('firefox').open_new_tab(urL)
解决方案 7:
这是两行代码!:D 你是一个伟大的程序员,所以永远不要放弃!
#Use web-browser.
import webbrowser as w
w.open("https://google.com")
#remember to include https://
#If you want to make a page open if you click a button do this :
from tkinter import *
#^ Imports tk
import webbrowser as w
#^ Imports wb
x = Tk()
#Makes main window
def clicked() :
w.open("https://google.com")
#Defined the click function. (We'll use this later.)
link = Button(x, text="Click Me!", command=clicked)
link.pack(pady=20, padx=20)
#Our button
x.mainloop()
#Tkinter mainloop
解决方案 8:
cmd
您可以使用任何可以与命令行( )进行交互的 Python 模块简单地实现它subprocess
,例如os
,等。但这里我仅提供了两个模块的示例。
这是语法(命令)cmd /c start browser_name "URL"
例子
import os
# or open with iexplore
os.system('cmd /c start iexplore "http://your_url"')
# or open with chrome
os.system('cmd /c start chrome "http://your_url"')
__import__('subprocess').getoutput('cmd /c start iexplore "http://your_url"')
您还可以在 cmd 中运行该命令,或者使用click
主要用于编写命令行实用程序的其他模块调用。
方法如下
import click
click.launch('http://your_url')
解决方案 9:
如果您想使用全屏或信息亭模式等命令行选项打开特定的浏览器(例如 Chrome 和 Chromium),并且还希望能够稍后将其终止,那么这可能适合您:
from threading import Timer
from time import sleep
import subprocess
import platform
# Hint 1: to enable F11 use --start-fullscreen instead of --kiosk, otherwise Alt+F4 to close the browser
# Hint 2: fullscreen will only work if chrome is not already running
platform_browser = {
'Windows': r'"C:Program Files (x86)GoogleChromeApplicationchrome.exe" --kiosk http://stackoverflow.com',
'Linux' : ['/usr/bin/chromium-browser', '--kiosk', 'http://stackoverflow.com']
}
browser = None
def open_browser():
global browser
platform_name = platform.system()
if platform_name in platform_browser:
browser = subprocess.Popen(platform_browser[platform_name])
else:
print(":-(")
Timer(1, open_browser).start() # delayed start, give e.g. your own web server time to launch
sleep(20) # start e.g. your python web server here instead
browser.kill()
解决方案 10:
如果你想打开任何网站,首先你需要导入一个名为“webbrowser”的模块。然后只需使用 webbrowser.open() 即可打开网站。例如
import webbrowser
webbrowser.open('https://yashprogrammer.wordpress.com/', new= 2)
扫码咨询,免费领取项目管理大礼包!