Hướng dẫn open chrome python - mở chrome python

    Hướng dẫn Python
  • Python bình luậnCommentsComments
  • Hoạt động giao diện điều khiển PythonConsole OperationsConsole Operations
  • Tuyên bố có điều kiện PythonConditional StatementsConditional Statements
  • Các câu lệnh Vòng lặp PythonLoop StatementsLoop Statements
  • Python enumEnumEnum
  • Các nhà khai thác PythonOperatorsOperators
  • Chức năng PythonFunctionsFunctions
  • Chức năng xây dựng PythonBuiltin FunctionsBuiltin Functions
  • Chuyển đổi loại PythonConversionConversion
  • Các lớp và đối tượng PythonClasses and ObjectsClasses and Objects
  • Chức năng toán học PythonMath FunctionsMath Functions

Nội dung chính ShowShow

  • Quá trình từng bước
  • Ví dụ 1: Mở URL trong Google Chrome
  • Ví dụ 2: Mở URL trong cửa sổ Google Chrome mới
  • Bản tóm tắt
  • Làm việc cho tôi trong Windows
  • Đã trả lời ngày 3 tháng 3 lúc 12:56
  • Trong selen để có được URL của tab đang hoạt động, hãy thử,
  • Làm cách nào để chạy mã python trong chrome?
  • Làm cách nào để sử dụng webbrowser trên Chrome với Python?
  • Làm cách nào để mở tệp HTML Python trong Chrome?
  • Chrome có thể chạy các tệp Python không?

Nội dung

  • Giới thiệu
  • Quá trình từng bước
  • Ví dụ 1: Mở URL trong Google Chrome
  • Ví dụ 2: Mở URL trong cửa sổ Google Chrome mới
  • Bản tóm tắt

Làm việc cho tôi trong Windows

Đã trả lời ngày 3 tháng 3 lúc 12:56

Quá trình từng bước

Ví dụ 1: Mở URL trong Google Chrome

  1. Ví dụ 2: Mở URL trong cửa sổ Google Chrome mới
  2. Bản tóm tắt

Ví dụ 1: Mở URL trong Google Chrome

Ví dụ 2: Mở URL trong cửa sổ Google Chrome mớihttps://pythonexamples.org in Chrome browser.

Bản tóm tắt

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open(url)

Làm việc cho tôi trong Windows

Đã trả lời ngày 3 tháng 3 lúc 12:56

Trong selen để có được URL của tab đang hoạt động, hãy thử,

Hướng dẫn open chrome python - mở chrome python

Ví dụ 2: Mở URL trong cửa sổ Google Chrome mới

Bản tóm tắt

Làm việc cho tôi trong Windowshttps://pythonexamples.org in a new Chrome browser.

Bản tóm tắt

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)

Làm việc cho tôi trong Windows

Đã trả lời ngày 3 tháng 3 lúc 12:56

Bản tóm tắt

Làm việc cho tôi trong Windows

Đã trả lời ngày 3 tháng 3 lúc 12:56

Trong selen để có được URL của tab đang hoạt động, hãy thử,Mar 17, 2014 at 0:50

1

Làm cách nào để chạy mã python trong chrome?

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)

Làm cách nào để sử dụng webbrowser trên Chrome với Python?

Làm cách nào để mở tệp HTML Python trong Chrome?1 gold badge30 silver badges34 bronze badges

Chrome có thể chạy các tệp Python không?Jun 22, 2014 at 17:28

Nội dungChad Jones

Giới thiệu1 gold badge10 silver badges3 bronze badges

6

import webbrowser 
new = 2 # open in a new tab, if possible

# open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.get(using='google-chrome').open(url,new=new)

Bạn có thể mở URL trong trình duyệt và cũng chỉ định để mở URL trong trình duyệt Chrome một cách cụ thể.

Để mở URL trong trình duyệt, chúng tôi sẽ sử dụng mô -đun Python ____24.Apr 24, 2017 at 21:21

njn1234njn1234njn1234njn1234

Ví dụ 2: Mở URL trong cửa sổ Google Chrome mớihttps://pythonexamples.org in Chrome browser. 2 silver badges5 bronze badges

1

Thực hiện theo các bước này trong ứng dụng Python của bạn để mở URL trong trình duyệt Chrome.

import webbrowser
chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'
webbrowser.get(chrome_path).open('http://docs.python.org/')

Bạn có thể mở URL trong trình duyệt và cũng chỉ định để mở URL trong trình duyệt Chrome một cách cụ thể.https://pythonexamples.org in a new Chrome browser.Jun 19, 2019 at 10:45

Theo tài liệu http://docs.python.org/3.3/l Library/webbrowser.html, nó được cho là mở trong trình duyệt mặc định, nhưng vì một số lý do trên máy của tôi, nó sẽ mở IE. Tôi đã thực hiện tìm kiếm trên google và tôi đã bắt gặp một câu trả lời nói rằng tôi cần đăng ký trình duyệt, nhưng tôi không chắc chắn làm thế nào để sử dụng webbrowser.register () và tài liệu dường như không rõ ràng. Làm cách nào để đăng ký chrome để các URL tôi chuyển đến webbrowser.open () Mở bằng chrome thay vì IE?

import webbrowser

webbrowser.open_new_tab("http://www.google.com")

hỏi ngày 17 tháng 3 năm 2014 lúc 0:50Mar 17, 2014 at 0:50Sep 24, 2019 at 2:02

Bạn có thể gọi get () với đường dẫn đến chrome. Dưới đây là một ví dụ - thay thế Chrome_Path bằng đường dẫn chính xác cho nền tảng của bạn.qloveshmily

Hassan Saeed8 silver badges5 bronze badges

5.3171 Huy hiệu vàng 30 Huy hiệu bạc34 Huy hiệu Đồng1 gold badge30 silver badges34 bronze badges

Đã trả lời ngày 22 tháng 6 năm 2014 lúc 17:28Jun 22, 2014 at 17:28only if you specifically need Chrome, and not the default browser, or Chromium, or something else.)

def try_find_chrome_path():
    result = None
    if _winreg:
        for subkey in ['ChromeHTML\\shell\\open\\command', 'Applications\\chrome.exe\\shell\\open\\command']:
            try: result = _winreg.QueryValue(_winreg.HKEY_CLASSES_ROOT, subkey)
            except WindowsError: pass
            if result is not None:
                result_split = shlex.split(result, False, True)
                result = result_split[0] if result_split else None
                if os.path.isfile(result):
                    break
                result = None
    else:
        expected = "google-chrome" + (".exe" if os.name == 'nt' else "")
        for parent in os.environ.get('PATH', '').split(os.pathsep):
            path = os.path.join(parent, expected)
            if os.path.isfile(path):
                result = path
                break
    return result

Chad Joneschad JonesChad JonesMay 26, 2019 at 8:10

user541686user541686user541686user541686

1.2811 Huy hiệu vàng10 Huy hiệu bạc3 Huy hiệu đồng1 gold badge10 silver badges3 bronze badges121 gold badges514 silver badges866 bronze badges

3

Bạn có thể sử dụng bất kỳ trình duyệt nào khác bằng cách thay đổi tham số 'bằng cách sử dụng' như được đưa ra trong một liên kết

import webbrowser
chrome_path = "C://Program Files (x86)//Google//Chrome//Application//Chrome.exe %s"
webbrowser.get(chrome_path).open("https://github.com/")

Đã trả lời ngày 24 tháng 4 năm 2017 lúc 21:21Apr 24, 2017 at 21:21

Đã trả lời ngày 11 tháng 1 năm 2021 lúc 7:08Jan 11, 2021 at 7:08Jan 11, 2021 at 7:08

Bạn cũng có thể sử dụng điều này:

import webbrowser

chrome_path = r"C:\Program Files\Google\Chrome\Application\chrome.exe"
url = "http://docs.python.org/"

webbrowser.register('chrome', None, webbrowser.BackgroundBrowser(chrome_path))
webbrowser.get('chrome').open_new_tab(url)

Đã trả lời ngày 16 tháng 3 năm 2021 lúc 17:22Mar 16, 2021 at 17:22Mar 16, 2021 at 17:22

1

Làm việc cho tôi trong Windows

Đặt đường dẫn của ứng dụng Chrome của bạn và đừng quên đặt %s vào cuối. Tôi vẫn đang cố gắng mở trình duyệt bằng mã HTML mà không lưu tệp ... Tôi sẽ thêm mã khi tôi tìm thấy cách.

import webbrowser
chromedir= "C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s"
webbrowser.get(chromedir).open("http://pythonprogramming.altervista.org")

>>> Liên kết đến: [Một trang từ blog của tôi nơi tôi giải thích điều này]

user7290573

1.3191 Huy hiệu vàng7 Huy hiệu bạc13 Huy hiệu đồng1 gold badge7 silver badges13 bronze badges1 gold badge7 silver badges13 bronze badges

Đã trả lời ngày 19 tháng 4 năm 2018 lúc 5:15Apr 19, 2018 at 5:15Apr 19, 2018 at 5:15

PythonProgrammipyThonProgrammiPythonProgrammiPythonProgrammi

21.1k3 Huy hiệu vàng39 Huy hiệu bạc34 Huy hiệu đồng3 gold badges39 silver badges34 bronze badges3 gold badges39 silver badges34 bronze badges

Nếu bạn đã đặt trình duyệt mặc định trong Windows thì bạn có thể làm điều này:

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
0

Đã trả lời ngày 5 tháng 7 năm 2019 lúc 14:58Jul 5, 2019 at 14:58Jul 5, 2019 at 14:58

1

Làm điều này cho một trò chơi tôi chơi, nó có liên quan nên tôi rời khỏi nó. Nó thực sự đơn giản. Lấy giá trị từ nền tảng.System. Kiểm tra nó chống lại các giá trị đã biết cho các hệ điều hành khác nhau. Nếu nó tìm thấy một trận đấu, nó sẽ đặt đường dẫn chrome cho bạn. Nếu không có ai được tìm thấy, nó sẽ mở trình duyệt mặc định vào liên kết của bạn. Hy vọng nó hữu ích cho ai đó.

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
1

Tôi thực sự đã thay đổi nó thêm một số ở đây nó được cập nhật vì tôi vẫn đang làm việc trên trình khởi chạy này

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
2

Đã trả lời ngày 22 tháng 11 năm 2019 lúc 6:48Nov 22, 2019 at 6:48Nov 22, 2019 at 6:48

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
3

100% công việc .... xem số dòng 535-545..change đường dẫn của Iexplore đến Firefox hoặc Chrome theo yêu cầu của bạn ... trong trường hợp thay đổi đường dẫn của tôi, tôi đã đề cập trong mã trên cho cài đặt Firefox ..... .

Đã trả lời ngày 31 tháng 5 năm 2020 lúc 8:07May 31, 2020 at 8:07May 31, 2020 at 8:07

SSCSWAPNILSSCSWAPNILSSCSWAPNILSSCSWAPNIL

5495 Huy hiệu bạc6 Huy hiệu Đồng5 silver badges6 bronze badges5 silver badges6 bronze badges

Tôi đã tìm thấy một câu trả lời cho câu hỏi của riêng tôi được đưa ra bởi câu trả lời của @Mehrdad bên dưới. Để truy vấn đường dẫn trình duyệt từ Windows theo cách chung @mehrdad đưa ra một mã ngắn đẹp sử dụng sổ đăng ký Windows, nhưng không bao gồm bối cảnh khá đủ để nó hoạt động.

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
4

Cảm ơn vì câu trả lời @Mehrdad !!

Đã trả lời ngày 29 tháng 11 năm 2019 lúc 8:36Nov 29, 2019 at 8:36Nov 29, 2019 at 8:36

Khi bạn có một URL không hợp lệ (đảm bảo rằng URL bắt đầu với https: // hoặc http: //, và nếu không, hãy thêm nó), nó thường mở mặc định IE.

Đã trả lời ngày 22 tháng 3 năm 2021 lúc 21:34Mar 22, 2021 at 21:34Mar 22, 2021 at 21:34

KetzoomerketzoomerKetZoomerKetZoomer

2.4863 huy hiệu vàng14 Huy hiệu bạc37 Huy hiệu đồng3 gold badges14 silver badges37 bronze badges3 gold badges14 silver badges37 bronze badges

Một cái gì đó như thế này nên hoạt động:

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
5

Ketzoomer

2.4863 huy hiệu vàng14 Huy hiệu bạc37 Huy hiệu đồng3 gold badges14 silver badges37 bronze badges3 gold badges14 silver badges37 bronze badges

Một cái gì đó như thế này nên hoạt động:Jun 23, 2017 at 7:35

KetzoomerLogan M

Một cái gì đó như thế này nên hoạt động:Jun 23, 2017 at 7:351 silver badge3 bronze badges

2

KetzoomerLogan M

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
6

Đã trả lời ngày 23 tháng 6 năm 2017 lúc 7:351 silver badge3 bronze badges

https://something.com

Logan Mlogan m

971 Huy hiệu bạc3 Huy hiệu đồng

tin tôi hay không đây là cách dễ nhất để làm

Lấy URL này chẳng hạnNOTE: If it has some another suffix like take https://meet.google.com for example if you try to add www. to it, your browser will throw a typo error )

Nếu bạn sẽ cung cấp URL này: https://somets.com hoặc something.com nó sẽ được mở trong IEMar 26, 2021 at 11:42

.PrabhavDevoPrabhavDevo

Nhưng nếu bạn sẽ gõ nó theo cách này: www.somothing.com nó sẽ được mở bằng ChromeNOTE: If it has some another suffix like take https://meet.google.com for example if you try to add www. to it, your browser will throw a typo error )6 silver badges21 bronze badges

Bạn có thể thử điều này và điều này sẽ hoạt động!Mar 26, 2021 at 11:42

  • Đã trả lời ngày 26 tháng 3 năm 2021 lúc 11:426 silver badges21 bronze badges
import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
7
  • Bohhavdevoprabhavdevo
import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
8

1.3606 huy hiệu bạc21 Huy hiệu đồng

Tôi nghĩ rằng tôi cũng đã tìm thấy một cách giải quyết

Phương pháp 1 (thay thế

import webbrowser 
new = 2 # open in a new tab, if possible

# open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.get(using='google-chrome').open(url,new=new)
0 bằng
import webbrowser 
new = 2 # open in a new tab, if possible

# open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.get(using='google-chrome').open(url,new=new)
1)♦21 gold badges75 silver badges127 bronze badges

Phương pháp 2 (sử dụng Oct 11, 2021 at 16:20

import webbrowser 
new = 2 # open in a new tab, if possible

# open a public URL, in this case, the webbrowser docs
url = "http://docs.python.org/library/webbrowser.html"
webbrowser.get(using='google-chrome').open(url,new=new)
2 như nhiều người trong số bạn đã đề xuất)21 gold badges75 silver badges127 bronze badgesPaul Lam

Dharman ♦Oct 11, 2021 at 16:202 silver badges11 bronze badges

Phù hiệu vàng 28K21Paul Lam

import webbrowser

url = 'https://pythonexamples.org'
webbrowser.register('chrome',
	None,
	webbrowser.BackgroundBrowser("C://Program Files (x86)//Google//Chrome//Application//chrome.exe"))
webbrowser.get('chrome').open_new(url)
9

Đã trả lời ngày 11 tháng 10 năm 2021 lúc 16:202 silver badges11 bronze badgesMar 3 at 12:56

Paul Lamlam Lam

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)
0

1462 Huy hiệu bạc11 Huy hiệu đồngMar 3 at 12:56

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)
1

Uhh ... hey bạn có thể nhanh chóng giải quyết vấn đề này bằng cách thêm https: // lemme hiển thị nó bên dưới -

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)
2

Đã trả lời ngày 3 tháng 3 lúc 12:56

Trong selen để có được URL của tab đang hoạt động, hãy thử,

Gửi tín hiệu để thay đổi tab

Và một lần nữa sử dụng

Tôi ở đây chỉ cung cấp một mã giả cho bạn.1 gold badge11 silver badges24 bronze badges

NickJun 28, 2017 at 10:24Jun 28, 2017 at 10:24

1.0321 Huy hiệu vàng11 Huy hiệu bạc24 Huy hiệu đồngAman RapariaAman Raparia

Đã trả lời ngày 28 tháng 6 năm 2017 lúc 10:241 gold badge5 silver badges13 bronze badges1 gold badge5 silver badges13 bronze badges

Aman Rapariaaman Raparia

4441 Huy hiệu vàng5 Huy hiệu bạc13 Huy hiệu đồng

Ít nhất trong Windows, nó phải là đủ và bạn không phải quan tâm đến đường dẫn đến trình duyệt.

import webbrowser

url = 'http://docs.python.org/'

# MacOS
chrome_path = 'open -a /Applications/Google\ Chrome.app %s'

# Windows
# chrome_path = 'C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s'

# Linux
# chrome_path = '/usr/bin/google-chrome %s'

webbrowser.get(chrome_path).open(url)
3Jun 11, 2017 at 15:29Jun 11, 2017 at 15:29

Làm cách nào để chạy mã python trong chrome?

Python mở Google...

#Open Google trong Python - Windows ..

Nhập Webbrowser ..

url='https://google.com'.

trình duyệt web.GET ('C:/Tệp chương trình (x86) /google/chrome/application/chrome.exe %s').Mở (URL).

Làm cách nào để sử dụng webbrowser trên Chrome với Python?

Python - Mở URL trong trình duyệt Chrome...

Đăng ký tên loại trình duyệt bằng Webbrowser.Đăng ký() .Cũng cung cấp đường dẫn tệp thực thi trình duyệt ..

Nhận đối tượng bộ điều khiển cho trình duyệt bằng WebBrowser.Nhận () và mở URL bằng Open () ..

Làm cách nào để mở tệp HTML Python trong Chrome?

Approach:...

Nhập mô -đun ..

Mở và tạo tệp ..

Thêm mã HTML ..

Ghi mã vào tệp ..

Đóng tập tin..

Mở tệp trong cửa sổ trình duyệt ..

Chrome có thể chạy các tệp Python không?

Python có thể là ngôn ngữ lập trình phổ biến nhất trên thế giới, nhưng không giống như các JavaScript hàng đầu khác, bạn không thể chạy mã Python trong trình duyệt.you can't run Python code in the browser.you can't run Python code in the browser.