在Python中打开网页,你可以使用以下几种方法:
1. 使用`webbrowser`模块:
import webbrowserurl = "http://www.baidu.com"webbrowser.open(url, new=0, autoraise=True) 在默认浏览器中打开webbrowser.open_new(url) 在新窗口中打开webbrowser.open_new_tab(url) 在新标签中打开
2. 使用`os`模块调用系统命令:
import osbrowser_path = "C:/Program Files/Internet Explorer/iexplore.exe" 替换为你的浏览器路径url = "http://www.baidu.com"os.system(f"{browser_path} {url}")

3. 使用`selenium`库(适用于需要自动化操作的情况):
from selenium import webdriverurl = "https://www.baidu.com"driver = webdriver.Chrome() 需要安装ChromeDriverdriver.get(url)time.sleep(5) 延迟5秒driver.quit() 关闭浏览器
4. 使用`urllib`或`requests`库(用于网页抓取或发送HTTP请求):
import requestsurl = "http://www.baidu.com"response = requests.get(url)print(response.text)
选择哪种方法取决于你的具体需求,例如是否需要自动化操作、是否需要处理cookies和重定向等。
