Thư viện webbrowser Python

The webbrowser module provides a high-level interface to allow displaying Web-based documents to users. Under most circumstances, simply calling the open() function from this module will open url using the default browser . You have to import the module and use open() function.

example

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.

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Made this for a game I play, it was relevant so i'm leaving it. It's real simple. Grabs the value from platform.system. Checks it against known values for different operating systems. If it finds a match it sets the chrome path for you. If none are found it opens default browser to your link. Hope its useful to someone.

import time
import os
import webbrowser
import platform

user_OS = platform.system()
chrome_path_windows = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
chrome_path_linux = '/usr/bin/google-chrome %s'
chrome_path_mac = 'open -a /Applications/Google\ Chrome.app %s'
chrome_path = ''
game_site_link = 'https://www.gamelink'

if user_OS == 'Windows':
    chrome_path = chrome_path_windows
elif user_OS == 'Linux':
    chrome_path = chrome_path_linux
elif user_OS == 'Darwin':
    chrome_path = chrome_path_mac
elif user_OS == 'Java':
    chrome_path = chrome_path_mac
else:
    webbrowser.open_new_tab(game_site_link)

webbrowser.get(chrome_path).open_new_tab(game_site_link) 

I actually changed it some more here it is updated since I am still working on this launcher

import time
import webbrowser
import platform
import subprocess
import os
import sys

privateServerLink = 'https://www.roblox.com/games/2414851778/TIER-20-Dungeon-Quest?privateServerLinkCode=GXVlmYh0Z7gwLPBf7H5FWk3ClTVesorY'
userBrowserC = input(str("Browser Type: chrome, opera, iexplore, firefox : "))
userSleepTime = int(input("How long do you want it to run?"))
if userBrowserC == 'opera':
    userBrowserD = 'launcher.exe'
else:
    userBrowserD = userBrowserC

if userBrowserC == "chrome":
    taskToKill = "chrome.exe"
else:
    taskToKill = "iexplore.exe"

if userBrowserC == 'chrome' and platform.system() == 'Windows':
     browserPath = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
elif userBrowserC == 'chrome' and platform.system() == 'Linux':
    browserPath = '/usr/bin/google-chrome %s'
elif userBrowserC == 'chrome' and platform.system() == 'Darwin' or 
platform.system() == 'Java':
    browserPath = 'open -a /Applications/Google\ Chrome.app %s'
elif userBrowserC == 'opera' and platform.system() == 'Windows':
    browserPath = 'C:/Users/'+ os.getlogin() +'/AppData/Local/Programs/Opera/launcher.exe'
elif userBrowserC == 'iexplore' and platform.system() == 'Windows':
    browserPath = 'C:/Program Files/internet explorer/iexplore.exe %s'
elif userBrowserC == 'firefox' and platform.system() == 'Windows':
    browserPath = 'C:/Program Files/Mozilla Firefox/firefox.exe'
else:
    browserPath = ''
while 1 == 1:   
    subprocess.Popen('SynapseX.exe')
    time.sleep(7)
    webbrowser.get(browserPath).open_new_tab(privateServerLink)
    time.sleep(7)  
    os.system('taskkill /f /im '+taskToKill)
    time.sleep(userSleepTime)

[tintuc]

Công việc nghiên cứu nhiều lúc cần tự động mở các trang web định sẳn chứa trong file text (file mở rộng *.txt). Sau gần nhiều năm nghiên cứu Admin mới tìm ra nên giới thiệu cho quý bạn tham khảo.

Nếu thấy bài viết hay nên share, like và comments. Thanks

Thư viện webbrowser Python
Dùng Python để lấy tài liệu




Ví muốn mở các trang chuyên về dược hóa sau:

google2.fda.gov
www.ema.europa.eu
kensaku.mhlw.go.jp
www.canada.ca
search.who.int
search.health.gov.au
mohfw.gov.in
patents.google.com
www.freepatentsonline.com
search.tga.gov.au
vinapharm.com.vn
www.google.com.vn
congbomypham.cqldvn.gov.vn
www.pharmacompass.com
api-generic.com
mail.google.com
www.cochrane.org
clinicaltrials.gov
archive.org
eurekasante.vidal.fr
www.sciencedirect.com
www.ebi.ac.uk
www.alibaba.com
www.sciencedirect.com
www.chemblink.com
www.pharmtech.com

Các bước tiến hành như sau:

Bước 1: Lưu các trang web vào file text (dùng notepad hoặc các phần mềm tương tự lưu). Xem hình

Thư viện webbrowser Python
Lưu các urls vào file text
Bước 2: Chuẩn bị Python

Cài đặc chương trình Python tải từ trang web https://www.python.org/ về

Cái đặt theo mặc định

Thư viện webbrowser Python
Tải Python
Bước 3: Viết đoạn lệnh Python sau:


import subprocess as sp
import webbrowser
import time

f = open('duoc_my_pham.txt','r')
urls = f.readlines()
for url in urls:
    child = sp.Popen(["C:/Program Files (x86)/Google/Chrome/Application/chrome.exe", url ])
    time.sleep(30)
    child.kill()


Lưu ý:

1. Tên "duoc_my_pham.txt" thay bằng tên file của mình tự đặt
2.  Dùng trình duyệt tùy thích
Ví dụ:
Google chrome: "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe"
Firefox: "C:\Program Files\Mozilla Firefox\firefox.exe"
....
3. Thời gian nhanh chậm khi mở một link bằng cách chỉnh số trong "    time.sleep(30)  "
4. Nếu không muốn nó tự đóng nên bỏ dòng "    child.kill()" đi.


Thư viện webbrowser Python
Đoạn mã chạy Python
Lưu đơạn mã trên thành file có phần mỡ rộng *.py

Có thể tải file mã tại đây

Cứ thế chương trình cứ chạy và mở các trang web mình cần xem

Có thể chạy trên Linux Ubuntu bằng lệnh sau:

Thư viện webbrowser Python
Chạy trên Ubuntu


import subprocess as sp
import webbrowser
import time

f = open('BioMed.txt','r')
urls = f.readlines()
for url in urls:
    child = sp.Popen(["firefox", url ])
    time.sleep(20)
    child.kill()


Bạn có thắc mắc gửi mail về [email protected] hoặc [email protected]
Chúc bạn thành công!



[/tintuc]