如何使用Python在我的Web浏览器中打开网站?

时间:2021-12-11 15:05:23

I want to open a website in my local computer's web browser (Chrome or Internet Explorer) using Python.

我想使用Python在本地计算机的网络浏览器(Chrome或Internet Explorer)中打开一个网站。

open("http://google.co.kr") # something like this  

Is there a module that can do this for me?

有没有可以为我做这个的模块?

7 个解决方案

#1


38  

The webbrowser module looks promising: https://www.youtube.com/watch?v=jU3P7qz3ZrM

webbrowser模块看起来很有前途:https://www.youtube.com/watch?v = jU3P7qz3ZrM

import webbrowser
webbrowser.open('http://google.co.kr', new=2)

#2


7  

As the instructions state, using the open() function does work, and opens the default web browser - usually I would say: "why wouldn't I want to use Firefox?!" (my default and favorite browser)

正如说明所述,使用open()函数确实有效,并打开默认的Web浏览器 - 通常我会说:“为什么我不想使用Firefox?!” (我的默认和最喜欢的浏览器)

import webbrowser as wb
wb.open_new_tab('http://www.google.com')

The above should work for the computer's default browser. However, what if you want to to open in Google Chrome?

以上内容适用于计算机的默认浏览器。但是,如果您想在Google Chrome中打开该怎么办?

The proper way to do this is:

正确的方法是:

import webbrowser as wb
wb.get('chrome %s').open_new_tab('http://www.google.com')

To be honest, I'm not really sure that I know the difference between 'chrome' and 'google-chrome', but apparently there is some since they've made the two different type names in the webbrowser documentation.

说实话,我不太确定我知道'chrome'和'google-chrome'之间的区别,但显然有一些因为他们在webbrowser文档中创建了两个不同的类型名称。

However, doing this didn't work right off the bat for me. Every time, I would get the error:

但是,这样做对我来说不起作用。每次,我都会收到错误:

Traceback (most recent call last):
File "C:\Python34\programs\a_temp_testing.py", line 3, in <module>
wb.get('google-chrome')
File "C:\Python34\lib\webbrowser.py", line 51, in get
raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

To solve this, I had to add the folder for chrome.exe to System PATH. My chrome.exe executable file is found at:

为了解决这个问题,我不得不将chrome.exe的文件夹添加到System PATH。我的chrome.exe可执行文件位于:

C:\Program Files (x86)\Google\Chrome\Application

You should check whether it is here or not for yourself.

您应该自己检查它是否在这里。

To add this to your Environment Variables System PATH, right click on your Windows icon and go to System. System Control Panel applet (Start - Settings - Control Panel - System). Change advanced settings, or the advanced tab, and select the button there called Environment Varaibles.

要将其添加到您的Environment Variables System PATH,请右键单击Windows图标并转到System。系统控制面板小程序(开始 - 设置 - 控制面板 - 系统)。更改高级设置或高级选项卡,然后选择名为Environment Varaibles的按钮。

Once you click on Environment Variables here, another window will pop up. Scroll through the items, select PATH, and click edit.

在此处单击Environment Variables后,将弹出另一个窗口。滚动项目,选择PATH,然后单击编辑。

Once you're in here, click New to add the folder path to your chrome.exe file. Like I said above, mine was found at:

进入此处后,单击“新建”将文件夹路径添加到chrome.exe文件中。就像我上面说的那样,我的发现是:

C:\Program Files (x86)\Google\Chrome\Application

Click save and exit out of there. Then make sure you reboot your computer.

单击“保存”并退出。然后确保重新启动计算机。

Hope this helps!

希望这可以帮助!

#3


7  

Actually it depends on what kind of uses. If you want to use it in a test-framework I highly recommend selenium-python. It is a great tool for testing automation related to web-browsers.

实际上它取决于什么样的用途。如果你想在测试框架中使用它,我强烈推荐selenium-python。它是测试与Web浏览器相关的自动化的绝佳工具。

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.python.org")

#4


6  

From the doc.

从文档。

The webbrowser module provides a high-level interface to allow displaying Web-based documents to users. Under most circumstances, simply calling the open() function from this module will do the right thing.

webbrowser模块提供了一个高级接口,允许向用户显示基于Web的文档。在大多数情况下,只需从此模块调用open()函数即可。

You have to import the module and use open() function.

您必须导入模块并使用open()函数。

To open in new tab:

要在新标签中打开:

import webbrowser
webbrowser.open('nabinkhadka.com.np', new = 2)

Also from the doc.

也来自doc。

If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible

如果new为0,则尽可能在同一浏览器窗口中打开URL。如果new为1,则尽可能打开新的浏览器窗口。如果new为2,则尽可能打开新的浏览器页面(“tab”)

So according to the value of new, you can either open page in same browser window or in new tab etc.

因此,根据new的值,您可以在同一浏览器窗口或新标签页中打开页面等。

Also you can specify as which browser (chrome, firebox, etc.) to open. Use get() function for this.

您还可以指定打开哪个浏览器(chrome,firebox等)。为此使用get()函数。

#5


3  

I think it should be

我认为应该是

import webbrowser

webbrowser.open('http://gatedin.com')

NOTE: make sure that you give http or https

注意:请确保您提供http或https

if you give "www." instead of "http:" instead of opening a broser the interprete displays boolean OutPut TRUE. here you are importing webbrowser library

如果你给“www。”而不是“http:”而不是打开更明亮的解释器显示布尔值OutPut TRUE。在这里您导入webbrowser库

#6


1  

import webbrowser

webbrowser.open("http://www.google.com")

The link will be opened in default web browser unless specified

除非指定,否则将在默认Web浏览器中打开该链接

#7


1  

If you want to open any website first you need to import a module called "webbrowser". Then just use webbrowser.open() to open a website. e.g.

如果您想首先打开任何网站,您需要导入一个名为“webbrowser”的模块。然后使用webbrowser.open()打开一个网站。例如

 import webbrowser

 webbrowser.open('https://yashprogrammer.wordpress.com/', new= 2)

#1


38  

The webbrowser module looks promising: https://www.youtube.com/watch?v=jU3P7qz3ZrM

webbrowser模块看起来很有前途:https://www.youtube.com/watch?v = jU3P7qz3ZrM

import webbrowser
webbrowser.open('http://google.co.kr', new=2)

#2


7  

As the instructions state, using the open() function does work, and opens the default web browser - usually I would say: "why wouldn't I want to use Firefox?!" (my default and favorite browser)

正如说明所述,使用open()函数确实有效,并打开默认的Web浏览器 - 通常我会说:“为什么我不想使用Firefox?!” (我的默认和最喜欢的浏览器)

import webbrowser as wb
wb.open_new_tab('http://www.google.com')

The above should work for the computer's default browser. However, what if you want to to open in Google Chrome?

以上内容适用于计算机的默认浏览器。但是,如果您想在Google Chrome中打开该怎么办?

The proper way to do this is:

正确的方法是:

import webbrowser as wb
wb.get('chrome %s').open_new_tab('http://www.google.com')

To be honest, I'm not really sure that I know the difference between 'chrome' and 'google-chrome', but apparently there is some since they've made the two different type names in the webbrowser documentation.

说实话,我不太确定我知道'chrome'和'google-chrome'之间的区别,但显然有一些因为他们在webbrowser文档中创建了两个不同的类型名称。

However, doing this didn't work right off the bat for me. Every time, I would get the error:

但是,这样做对我来说不起作用。每次,我都会收到错误:

Traceback (most recent call last):
File "C:\Python34\programs\a_temp_testing.py", line 3, in <module>
wb.get('google-chrome')
File "C:\Python34\lib\webbrowser.py", line 51, in get
raise Error("could not locate runnable browser")
webbrowser.Error: could not locate runnable browser

To solve this, I had to add the folder for chrome.exe to System PATH. My chrome.exe executable file is found at:

为了解决这个问题,我不得不将chrome.exe的文件夹添加到System PATH。我的chrome.exe可执行文件位于:

C:\Program Files (x86)\Google\Chrome\Application

You should check whether it is here or not for yourself.

您应该自己检查它是否在这里。

To add this to your Environment Variables System PATH, right click on your Windows icon and go to System. System Control Panel applet (Start - Settings - Control Panel - System). Change advanced settings, or the advanced tab, and select the button there called Environment Varaibles.

要将其添加到您的Environment Variables System PATH,请右键单击Windows图标并转到System。系统控制面板小程序(开始 - 设置 - 控制面板 - 系统)。更改高级设置或高级选项卡,然后选择名为Environment Varaibles的按钮。

Once you click on Environment Variables here, another window will pop up. Scroll through the items, select PATH, and click edit.

在此处单击Environment Variables后,将弹出另一个窗口。滚动项目,选择PATH,然后单击编辑。

Once you're in here, click New to add the folder path to your chrome.exe file. Like I said above, mine was found at:

进入此处后,单击“新建”将文件夹路径添加到chrome.exe文件中。就像我上面说的那样,我的发现是:

C:\Program Files (x86)\Google\Chrome\Application

Click save and exit out of there. Then make sure you reboot your computer.

单击“保存”并退出。然后确保重新启动计算机。

Hope this helps!

希望这可以帮助!

#3


7  

Actually it depends on what kind of uses. If you want to use it in a test-framework I highly recommend selenium-python. It is a great tool for testing automation related to web-browsers.

实际上它取决于什么样的用途。如果你想在测试框架中使用它,我强烈推荐selenium-python。它是测试与Web浏览器相关的自动化的绝佳工具。

from selenium import webdriver

driver = webdriver.Firefox()
driver.get("http://www.python.org")

#4


6  

From the doc.

从文档。

The webbrowser module provides a high-level interface to allow displaying Web-based documents to users. Under most circumstances, simply calling the open() function from this module will do the right thing.

webbrowser模块提供了一个高级接口,允许向用户显示基于Web的文档。在大多数情况下,只需从此模块调用open()函数即可。

You have to import the module and use open() function.

您必须导入模块并使用open()函数。

To open in new tab:

要在新标签中打开:

import webbrowser
webbrowser.open('nabinkhadka.com.np', new = 2)

Also from the doc.

也来自doc。

If new is 0, the url is opened in the same browser window if possible. If new is 1, a new browser window is opened if possible. If new is 2, a new browser page (“tab”) is opened if possible

如果new为0,则尽可能在同一浏览器窗口中打开URL。如果new为1,则尽可能打开新的浏览器窗口。如果new为2,则尽可能打开新的浏览器页面(“tab”)

So according to the value of new, you can either open page in same browser window or in new tab etc.

因此,根据new的值,您可以在同一浏览器窗口或新标签页中打开页面等。

Also you can specify as which browser (chrome, firebox, etc.) to open. Use get() function for this.

您还可以指定打开哪个浏览器(chrome,firebox等)。为此使用get()函数。

#5


3  

I think it should be

我认为应该是

import webbrowser

webbrowser.open('http://gatedin.com')

NOTE: make sure that you give http or https

注意:请确保您提供http或https

if you give "www." instead of "http:" instead of opening a broser the interprete displays boolean OutPut TRUE. here you are importing webbrowser library

如果你给“www。”而不是“http:”而不是打开更明亮的解释器显示布尔值OutPut TRUE。在这里您导入webbrowser库

#6


1  

import webbrowser

webbrowser.open("http://www.google.com")

The link will be opened in default web browser unless specified

除非指定,否则将在默认Web浏览器中打开该链接

#7


1  

If you want to open any website first you need to import a module called "webbrowser". Then just use webbrowser.open() to open a website. e.g.

如果您想首先打开任何网站,您需要导入一个名为“webbrowser”的模块。然后使用webbrowser.open()打开一个网站。例如

 import webbrowser

 webbrowser.open('https://yashprogrammer.wordpress.com/', new= 2)