如何在python中打开一个url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4302027/
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-18 15:09:58  来源:igfitidea点击:

how to open a url in python

pythonbottle

提问by shamsee

import urllib

fun open():
    return urllib.urlopen('http://example.com')

But when example.com opens it does not render css or js. How can I open the webpage in a web browser?

但是当 example.com 打开时,它不会呈现 css 或 js。如何在网络浏览器中打开网页?

@error(404)
def error404(error):
    return webbrowser.open('http://example.com')

I am using bottle. Giving me the error: TypeError("'bool' object is not iterable",)

我正在使用瓶子。给我错误:TypeError("'bool' object is not iterable",)

采纳答案by aaronasterling

with the webbrowsermodule

使用网络浏览器模块

import webbrowser

webbrowser.open('http://example.com')  # Go to example.com

回答by pyfunc

You have to read the data too.

您也必须读取数据。

Check out : http://www.doughellmann.com/PyMOTW/urllib2/to understand it.

查看:http: //www.doughellmann.com/PyMOTW/urllib2/以了解它。

response = urllib2.urlopen(..)
headers = response.info()
data = response.read()

Of course, what you want is to render it in browser and aaronasterling's answer is what you want.

当然,您想要的是在浏览器中呈现它,而 aaronasterling 的答案就是您想要的。

回答by imp

import webbrowser  
webbrowser.open(url, new=0, autoraise=True)

Display url using the default browser. 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. If autoraise is True, the window is raised

使用默认浏览器显示 url。如果 new 为 0,则尽可能在同一浏览器窗口中打开 url。如果 new 为 1,则可能会打开一个新的浏览器窗口。如果 new 为 2,则可能会打开一个新的浏览器页面(“标签”)。如果 autoraise 为 True,则提升窗口

webbrowser.open_new(url)

Open url in a new window of the default browser

在默认浏览器的新窗口中打开 url

webbrowser.open_new_tab(url)

Open url in a new page (“tab”) of the default browser

在默认浏览器的新页面(“标签”)中打开 url

回答by Sebastian Hietsch

You could also try:

你也可以试试:

import os
os.system("start \"\" http://example.com")

This, other than @aaronasterling ′s answer has the advantage that it opens the default web browser. Be sure not to forget the "http://".

这与@aaronasterling 的答案不同,它的优势在于它打开了默认的 Web 浏览器。一定不要忘记“http://”。

回答by David Odhiambo

Here is another way to do it.

这是另一种方法。

import webbrowser

webbrowser.open("foobar.com")

回答by Namdari Himan

I think this is the easy way to open a URL using this function

我认为这是使用此功能打开 URL 的简单方法

webbrowser.open_new_tab(url)

回答by Abraham Hernandez

On window

窗口

import os
os.system("start \"\" https://example.com")

On macOS

macOS 上

import os
os.system("open \"\" https://example.com")

On Linux

Linux 上

import os
os.system("xdg-open \"\" https://example.com")

Cross-Platform

跨平台

import webbrowser

webbrowser.open('https://example.com')