网站首页 > 技术文章 正文

如何通过代码启动系统浏览器访问一个网页
一行代码可以实现,打开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 实现函数的异步启动和异步结束
- 277℃Python短文,Python中的嵌套条件语句(六)
- 276℃python笔记:for循环嵌套。end=""的作用,图形打印
- 273℃PythonNet:实现Python与.Net代码相互调用!
- 268℃Python实现字符串小写转大写并写入文件
- 267℃Python操作Sqlserver数据库(多库同时异步执行:增删改查)
- 126℃原来2025是完美的平方年,一起探索六种平方的算吧
- 110℃Ollama v0.4.5-v0.4.7 更新集合:Ollama Python 库改进、新模型支持
- 108℃Python 和 JavaScript 终于联姻了!PythonMonkey 要火?
- 最近发表
-
- 假期苦短,我用Python!这有个自动回复拜年信息的小程序
- 第4天|16天搞定Python数据分析,图表,靓靓靓
- 60带源代码的Python项目之 08 使用 Python 生成 QR 码
- 用python3 实现生成二维码代码示例
- python批量生成二维码图片(python如何生成二维码)
- 使用python生成二维码(python生成二维码界面)
- Python制作二维码(python制作二维码小作业)
- Python 生成条形码、二维码 (Code 128、EAN-13、QR code等)
- python如何实现二维码的生成和识别
- Python 使用 JsonPath 完成接口自动化测试中参数关联和数据验证
- 标签列表
-
- 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)