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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31715119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:27:43  来源:igfitidea点击:

How can I open a website in my web browser using Python?

python

提问by ???

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

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

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

Is there a module that can do this for me?

有没有模块可以为我做到这一点?

采纳答案by csiu

The webbrowsermodule 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)

回答by Fu Jian

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")

回答by Nabin

From the doc.

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.

也来自文档。

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()函数。

回答by ntk4

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 的文件夹添加到系统路径中。我的 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.

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

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

单击此处的环境变量后,将弹出另一个窗口。滚动项目,选择 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!

希望这可以帮助!

回答by vijaya karthavya kudithipudi

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 webbrowserlibrary

如果你给出“ www.”而不是“ http:”而不是打开一个broser,解释器会显示布尔值 OutPut TRUE。在这里你正在导入webbrowser

回答by Krishna Kattula

import webbrowser

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

The link will be opened in default web browser unless specified

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

回答by Yash Tile

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)

回答by Ali Moshiri

I had this problem.When I define firefox path my problem had been solved.

我有这个问题。当我定义 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)

回答by A.J.Bauer

If you want to open a specific browser (e.g. Chrome and Chromium) with command line options like full screen or kiosk mode and also want to be able to kill it later on, then this might work for you:

如果您想使用全屏或自助服务终端模式等命令行选项打开特定浏览器(例如 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)\Google\Chrome\Application\chrome.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()