Hướng dẫn python html viewer - trình xem html python

Tôi đang cố gắng mở một tệp HTML từ Python nhưng tập lệnh của tôi chỉ hiển thị nội dung của tệp HTML trong Python thay vì mở nó trong trình duyệt. Làm thế nào tôi có thể khắc phục sự cố này? Làm cách nào để mở tệp HTML trong trình duyệt Chrome của mình?

Nội phân chính

  • Làm cách nào để mở một tệp HTML trong Python?
  • Làm cách nào để xem tệp HTML trong trình duyệt?
  • Tôi có thể kết nối HTML với Python không?
  • Làm cách nào để mở một trang web trong một trình duyệt cụ thể bằng Python?

testdata.html

Hướng dẫn python html viewer - trình xem html python

Tập lệnh Python 2.7:

import urllib
page =  urllib.urlopen('testdata.html').read()
print page

Hướng dẫn python html viewer - trình xem html python

Danielo515

4.5732 Huy hiệu vàng28 Huy hiệu bạc51 Huy hiệu Đồng2 gold badges28 silver badges51 bronze badges

Đã hỏi ngày 1 tháng 12 năm 2016 lúc 8:21Dec 1, 2016 at 8:21

1

Hãy thử chỉ định "Tệp: //" khi bắt đầu URL.

// Also, use the absolute path of the file:

webbrowser.open('file://' + os.path.realpath(filename))

Hoặc

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.open(url,new=new)

// open an HTML file on my own (Windows) computer
url = "file://d/testdata.html"
webbrowser.open(url,new=new)

Đã trả lời ngày 1 tháng 12 năm 2016 lúc 8:27Dec 1, 2016 at 8:27

3

import os
os.system("start [your's_url]")

Enjoy!

Đã trả lời ngày 1 tháng 12 năm 2016 lúc 8:24Dec 1, 2016 at 8:24

Yuval Prussyuval PrussYuval Pruss

7.65012 Huy hiệu vàng39 Huy hiệu bạc65 Huy hiệu Đồng12 gold badges39 silver badges65 bronze badges

2

Bạn có thể sử dụng Thư viện Webbrowser:webbrowser library:

import webbrowser
url = 'file:///path/to/your/file/testdata.html'
webbrowser.open(url, new=2)  # open in new tab

Đã trả lời ngày 1 tháng 12 năm 2016 lúc 8:32Dec 1, 2016 at 8:32

Đây là một cách không yêu cầu các thư viện bên ngoài và cũng có thể hoạt động của các tệp cục bộ.

import subprocess
import os

url = "https://stackoverflow.com"
# or a file on your computer
# url = "/Users/yourusername/Desktop/index.html
try: # should work on Windows
    os.startfile(url)
except AttributeError:
    try: # should work on MacOS and most linux versions
        subprocess.call(['open', url])
    except:
        print('Could not open URL')

Đã trả lời ngày 23 tháng 5 năm 2020 lúc 6:34May 23, 2020 at 6:34

WyattbluewyattblueWyattBlue

4991 Huy hiệu vàng5 Huy hiệu bạc18 Huy hiệu đồng1 gold badge5 silver badges18 bronze badges

Bạn có thể sử dụng selen.

Tải xuống crômedriver mới nhất, dán crômedriver.exe trong "C: \ python27 \ scripts".

sau đó

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("your page path")
print driver.page_source.encode('utf-8')
driver.quit()
display.stop()

Đã trả lời ngày 1 tháng 12 năm 2016 lúc 8:36Dec 1, 2016 at 8:36

RainmakerrainmakerRainmaker

9.3366 huy hiệu vàng49 Huy hiệu bạc78 Huy hiệu đồng6 gold badges49 silver badges78 bronze badges

Tôi cảm thấy đây là giải pháp dễ nhất:

import os

os.getcwd() #To check the current working directory or path
os.chdir("D:\\Folder Name\\") # D:\Folder Name\ is the new path where you want to save the converted dataframe(df) to .html file

import webbrowser

df.to_html("filename.html") #Converting dataframe df to html and saving with a name 'filename' and 
webbrowser.get("C:/Program Files (x86)/Google/Chrome/Application/chrome.exe %s").open("file://" + os.path.realpath("filename.html"))

snwflk

3.1314 Huy hiệu vàng22 Huy hiệu bạc36 Huy hiệu đồng4 gold badges22 silver badges36 bronze badges

Đã trả lời ngày 26 tháng 10 năm 2020 lúc 16:35Oct 26, 2020 at 16:35

GoutamgoutamGoutam

3591 Huy hiệu vàng2 Huy hiệu bạc9 Huy hiệu đồng1 gold badge2 silver badges9 bronze badges

Bạn có thể tải xuống phiên bản mới nhất của "Gecodriver" từ đây. Sau đó thêm tệp thực thi Gecodriver vào dự án của bạn. Sau đó, PIP cài đặt Selenium và bên dưới mã cho Windows:

from selenium import webdriver   
from selenium.webdriver.firefox.options import Options   
import os

#optional
options = Options()   
options.set_preference('permissions.default.image', 2)   
options.set_preference('dom.ipc.plugins.enabled.libflashplayer.so', False)   

#for windows
Driver = webdriver.Firefox(options=options, executable_path='geckodriver.exe')   
Driver.implicitly_wait(15)

#path of your project -> reference : "https://stackoverflow.com/questions/25389095/python-get-path-of-root-project-structure/40227116"   
Root = os.path.dirname(os.path.abspath(__file__))    
driver.get('file://' + Root + 'path/to/htmlfile')

Hy vọng tôi đã giúp bạn :)

Đã trả lời ngày 3 tháng 3 năm 2020 lúc 20:49Mar 3, 2020 at 20:49

2

import urllib
page =  urllib.urlopen('testdata.html').read()
print page
0

Đã trả lời ngày 19 tháng 11 năm 2020 lúc 11:41Nov 19, 2020 at 11:41

1

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

Open () để mở tệp HTML trong Python. Cuộc gọi codec. Mở (tên tệp, chế độ, mã hóa) với tên tệp là tên của tệp HTML, chế độ là "R" và mã hóa là "UTF-8" để mở tệp HTML ở chế độ chỉ đọc. to open an HTML file within Python. Call codecs. open(filename, mode, encoding) with filename as the name of the HTML file, mode as "r" , and encoding as "utf-8" to open an HTML file in read-only mode.

Làm cách nào để xem tệp HTML trong trình duyệt?

Mở tệp HTML đã lưu trong trình duyệt yêu thích của bạn (nhấp đúp vào tệp hoặc nhấp chuột phải - và chọn "Mở bằng").double click on the file, or right-click - and choose "Open with").

Tôi có thể kết nối HTML với Python không?

Làm cách nào để mở một trang web trong một trình duyệt cụ thể bằng Python?. You can also use Python to work with this data directly.

Làm cách nào để mở một trang web trong một trình duyệt cụ thể bằng Python?

Tập lệnh Python 2.7:use the webbrowser. get() function to specify a particular browser.