Mô-đun cung cấp giao diện cấp cao để cho phép hiển thị các tài liệu dựa trên web cho người dùng. Trong hầu hết các trường hợp, chỉ cần gọi hàm từ mô-đun này sẽ làm đúng
Trong Unix, trình duyệt đồ họa được ưa thích hơn trong X11, nhưng trình duyệt ở chế độ văn bản sẽ được sử dụng nếu trình duyệt đồ họa không khả dụng hoặc màn hình X11 không khả dụng. Nếu sử dụng trình duyệt chế độ văn bản, quá trình gọi sẽ bị chặn cho đến khi người dùng thoát khỏi trình duyệt
Nếu biến môi trường
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]3 tồn tại, nó được hiểu là danh sách các trình duyệt được phân tách để thử trước các giá trị mặc định của nền tảng. Khi giá trị của một phần danh sách chứa chuỗi
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]4, thì nó được hiểu là một dòng lệnh trình duyệt theo nghĩa đen được sử dụng với URL đối số được thay thế cho
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]4; .
Đối với các nền tảng không phải Unix hoặc khi trình duyệt từ xa khả dụng trên Unix, quy trình kiểm soát sẽ không đợi người dùng kết thúc với trình duyệt mà cho phép trình duyệt từ xa duy trì các cửa sổ của riêng nó trên màn hình. Nếu các trình duyệt từ xa không khả dụng trên Unix, quá trình kiểm soát sẽ khởi chạy một trình duyệt mới và đợi
Trình duyệt web tập lệnh có thể được sử dụng làm giao diện dòng lệnh cho mô-đun. Nó chấp nhận một URL làm đối số. Nó chấp nhận các tham số tùy chọn sau.
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]0 mở URL trong cửa sổ trình duyệt mới, nếu có thể; . Các tùy chọn, một cách tự nhiên, loại trừ lẫn nhau. Ví dụ sử dụng
python -m webbrowser -t "//www.python.org"
không phải Emscripten, không phải WASI
Mô-đun này không hoạt động hoặc không khả dụng trên nền tảng WebAssugging
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]2 và
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]3. Xem để biết thêm thông tin
Ngoại lệ sau đây được xác định
ngoại lệ trình duyệt web. LỗiNgoại lệ được đưa ra khi xảy ra lỗi kiểm soát trình duyệt
Các chức năng sau đây được xác định
trình duyệt web. mở[url , mới=0 , autoraise=Đúng]Hiển thị url bằng trình duyệt mặc định. Nếu mới là 0, url sẽ được mở trong cùng một cửa sổ trình duyệt nếu có thể. Nếu mới là 1, một cửa sổ trình duyệt mới sẽ được mở nếu có thể. Nếu mới là 2, một trang trình duyệt mới [“tab”] sẽ được mở nếu có thể. Nếu autoraise là
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]4, cửa sổ sẽ được nâng lên nếu có thể [lưu ý rằng trong nhiều trình quản lý cửa sổ, điều này sẽ xảy ra bất kể cài đặt của biến này]
Lưu ý rằng trên một số nền tảng, cố gắng mở tên tệp bằng chức năng này, có thể hoạt động và khởi động chương trình liên kết của hệ điều hành. Tuy nhiên, điều này không được hỗ trợ cũng như di động
Tăng một
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]5 với đối số
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]6trình duyệt web. open_new[url]
Mở url trong một cửa sổ mới của trình duyệt mặc định, nếu có thể, nếu không, hãy mở url trong cửa sổ trình duyệt duy nhất
trình duyệt web. open_new_tab[url]Mở url trong một trang mới [“tab”] của trình duyệt mặc định, nếu có thể, nếu không thì tương đương với
trình duyệt web. nhận[đang sử dụng=Không có]Trả về một đối tượng bộ điều khiển cho loại trình duyệt bằng cách sử dụng. Nếu sử dụng là
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]8, hãy trả lại bộ điều khiển cho trình duyệt mặc định phù hợp với môi trường của người gọitrình duyệt web. đăng ký[tên , hàm tạo , trường hợp=Không , * , ưa thích=Sai]
Đăng ký tên loại trình duyệt. Khi một loại trình duyệt được đăng ký, hàm có thể trả về bộ điều khiển cho loại trình duyệt đó. Nếu phiên bản không được cung cấp hoặc là
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]8, hàm tạo sẽ được gọi mà không có tham số để tạo phiên bản khi cần. Nếu thể hiện được cung cấp, hàm tạo sẽ không bao giờ được gọi và có thể là
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]8
Cài đặt ưu tiên thành
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]4 làm cho trình duyệt này trở thành kết quả ưu tiên cho cuộc gọi không có đối số. Mặt khác, điểm vào này chỉ hữu ích nếu bạn định đặt biến
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]3 hoặc gọi với đối số không trống khớp với tên của trình xử lý mà bạn khai báo
Đã thay đổi trong phiên bản 3. 7. tham số chỉ từ khóa ưa thích đã được thêm vào.
Một số loại trình duyệt được xác định trước. Bảng này cung cấp các tên loại có thể được truyền cho hàm và các phần khởi tạo tương ứng cho các lớp trình điều khiển, tất cả được định nghĩa trong mô-đun này
Tên loại
Tên lớp
ghi chú
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]87
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]88
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]89
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]88
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]81
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]82
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]83
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]84
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]85
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]86
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]87
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]88
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]89
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]10
[1]
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]11
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]10
[1]
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]13
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]10
[1]
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]15
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]16
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]17
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]18
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]19
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]30
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]31
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]32
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]33
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]34
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]35
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]36
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]37
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]38
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]39
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]10
[2]
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]11
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]12
[3]
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]13
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]14
[3]
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]15
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]16
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]17
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]18
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]19
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]40
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]41
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]42
ghi chú
“Konqueror” là trình quản lý tệp cho môi trường máy tính để bàn KDE dành cho Unix và chỉ có ý nghĩa khi sử dụng nếu KDE đang chạy. Một số cách phát hiện KDE đáng tin cậy sẽ rất hay; . Cũng lưu ý rằng tên “kfm” được sử dụng ngay cả khi sử dụng lệnh konqueror với KDE 2 — quá trình triển khai chọn chiến lược tốt nhất để chạy Konqueror
Chỉ trên nền tảng Windows
Chỉ trên nền tảng macOS
Mới trong phiên bản 3. 3. Đã thêm hỗ trợ cho Chrome/Chromium.
Không dùng nữa kể từ phiên bản 3. 11, sẽ bị xóa trong phiên bản 3. 13. ______144 không được dùng nữa, thay vào đó hãy sử dụng
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]45.
Dưới đây là một số ví dụ đơn giản
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]
Đối tượng điều khiển trình duyệt
Bộ điều khiển trình duyệt cung cấp các phương thức này song song với ba chức năng tiện lợi ở cấp độ mô-đun
trình duyệt web. tênTên phụ thuộc hệ thống cho trình duyệt
bộ điều khiển. mở[url , mới=0, autoraise=True]Hiển thị url bằng trình duyệt do bộ điều khiển này xử lý. Nếu mới là 1, một cửa sổ trình duyệt mới sẽ được mở nếu có thể. Nếu mới là 2, một trang trình duyệt mới [“tab”] sẽ được mở nếu có thể
bộ điều khiển. open_new[url]Mở url trong một cửa sổ mới của trình duyệt do bộ điều khiển này xử lý, nếu có thể, nếu không, hãy mở url trong cửa sổ trình duyệt duy nhất. bí danh
bộ điều khiển. open_new_tab[url]Mở url trong một trang mới [“tab”] của trình duyệt do bộ điều khiển này xử lý, nếu có thể, nếu không thì tương đương với
chú thích
Các tệp thực thi có tên ở đây không có đường dẫn đầy đủ sẽ được tìm kiếm trong các thư mục được cung cấp trong biến môi trường
url = '//docs.python.org/' # Open URL in a new tab, if a browser window is already open. webbrowser.open_new_tab[url] # Open URL in new window, raising the window if possible. webbrowser.open_new[url]48