网站首页 > 技术文章 正文

如何通过代码启动系统浏览器访问一个网页
一行代码可以实现,打开python官网
python -m webbrowser -t "http://www.python.org"
python -m webbrowser -t "http://www.baidu.com"

输入命令,回车后,python程序会找到系统的浏览器,本人电脑自动找到的edge浏览器,由于python.org打开很慢,换成了国内的百度

使用编程的方式打开浏览器
import webbrowser
webbrowser.open("https://www.baidu.com")
使用webbrowser的open方法启动浏览器打开一个网址
webbrowser.open(url, new=0, autoraise=True)
方法作用 new参数的作用,其实是就是控制浏览器打开网页是开新浏览器,还是在新标签中
- 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.
使用其他浏览器如firefox
c = webbrowser.get('firefox')
c.open('http://www.baidu.org')
c.open_new_tab('http://docs.baidu.org')
实际上在我的电脑上是不支持的。即使使用register方法也是有问题的
尝试了手动注册一个firefox也是没有效果的,目前还未发现喝到的原因
看哪位网友能找到具体的原因,同时反馈一下。
firefox='C:\Program Files\Mozilla Firefox\firefox.exe'
# webbrowser.register('firefox', Mozilla.__class__, webbrowser.GenericBrowser('firefox'))
# ff_path = webbrowser.get(firefox)
# ff = webbrowser.get(ff_path)
webbrowser.register("firefox", None, webbrowser.BackgroundBrowser(firefox))
# webbrowser.register('firefox', None, ff)
c = webbrowser.get( "firefox")
print(c)
print("============")
c.open("https://www.baidu.com")
猜你喜欢
- 2024-12-26 在Python中使用Asyncio系统(3-6)?优雅地开启和关闭协程
- 2024-12-26 树莓派开机启动脚本
- 2024-12-26 Python 30 天探索:文件读取的奥秘之旅
- 2024-12-26 Python 面向对象编程:开启高效编程之旅
- 2024-12-26 python 启动EXCEL执行宏关闭EXCEL 定时器
- 2024-12-26 PHP/Python如何开启自带的静态文件 web 服务器
- 2024-12-26 一篇文章搞定Django项目上线与部署方法
- 2024-12-26 青少年之Python编程课程安排
- 2024-12-26 python:快速启动-android稳定性测试
- 2024-12-26 Python 实现函数的异步启动和异步结束
- 05-25Python 3.14 t-string 要来了,它与 f-string 有何不同?
- 05-25Python基础元素语法总结
- 05-25Python中的变量是什么东西?
- 05-25新手常见的python报错及解决方案
- 05-2511-Python变量
- 05-2510个每个人都是需要知道Python问题
- 05-25Python编程:轻松掌握函数定义、类型及其参数传递方式
- 05-25Python基础语法
- 257℃Python短文,Python中的嵌套条件语句(六)
- 257℃python笔记:for循环嵌套。end=""的作用,图形打印
- 256℃PythonNet:实现Python与.Net代码相互调用!
- 251℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 251℃Python实现字符串小写转大写并写入文件
- 106℃原来2025是完美的平方年,一起探索六种平方的算吧
- 90℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 81℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 最近发表
- 标签列表
-
- python中类 (31)
- python 迭代 (34)
- python 小写 (35)
- python怎么输出 (33)
- python 日志 (35)
- python语音 (31)
- python 工程师 (34)
- python3 安装 (31)
- python音乐 (31)
- 安卓 python (32)
- python 小游戏 (32)
- python 安卓 (31)
- python聚类 (34)
- python向量 (31)
- python大全 (31)
- python次方 (33)
- python桌面 (32)
- python总结 (34)
- python浏览器 (32)
- python 请求 (32)
- python 前端 (32)
- python验证码 (33)
- python 题目 (32)
- python 文件写 (33)
- python中的用法 (32)