Hướng dẫn python print html pretty - python in html khá đẹp

Tôi đang sử dụng lxml.html để tạo ra một số HTML. Tôi muốn in đẹp (với thụt lề) kết quả cuối cùng của tôi vào một tệp HTML. Làm thế nào để làm điều đó?

Đây là những gì tôi đã thử và có được cho đến bây giờ

import lxml.html as lhfrom lxml.html import builder as EsliderRoot=lh.Element("div", E.CLASS("scroll"), style="overflow-x: hidden; overflow-y: hidden;")scrollContainer=lh.Element("div", E.CLASS("scrollContainer"), style="width: 4340px;")sliderRoot.append(scrollContainer)print lh.tostring(sliderRoot, pretty_print = True, method="html")

Như bạn có thể thấy tôi đang sử dụng thuộc tính

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
2. Tôi nghĩ rằng sẽ cung cấp mã thụt lề, nhưng nó không thực sự giúp ích. Đây là đầu ra:

Hướng dẫn python print html pretty - python in html khá đẹp

Hướng dẫn python in đẹp html

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
3

OneCricketeer

160K18 Huy hiệu vàng119 Huy hiệu bạc219 Huy hiệu đồng18 gold badges119 silver badges219 bronze badges

Hướng dẫn python print html pretty - python in html khá đẹp

Hướng dẫn python in đẹp html

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
3May 27, 2011 at 9:09

0

OneCricketeer

Hướng dẫn python print html pretty - python in html khá đẹp

Hướng dẫn python in đẹp html

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
3

OneCricketeer

160K18 Huy hiệu vàng119 Huy hiệu bạc219 Huy hiệu đồng

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html

Khi được hỏi ngày 27 tháng 5 năm 2011 lúc 9:09

Tôi đã kết thúc việc sử dụng đẹp trực tiếp. Đó là một cái gì đó lxml.html.soupparser sử dụng để phân tích cú pháp HTML.1 gold badge10 silver badges16 bronze badges

Đẹp có một phương pháp đẹp mắt thực hiện chính xác những gì nó nói nó làm. Nó làm đẹp HTML với các vết thương và mọi thứ thích hợp.May 29, 2011 at 11:14

Đẹp sẽ không sửa HTML, vì vậy mã bị hỏng, vẫn bị hỏng. Nhưng trong trường hợp này, vì mã được tạo bởi LXML, mã HTML ít nhất phải đúng về mặt ngữ nghĩa.bcosynot

Trong ví dụ được đưa ra trong câu hỏi của tôi, tôi sẽ phải làm điều này:9 gold badges34 silver badges45 bronze badges

5

Tyrannas

Phim thương hiệu vàng 3,7931

Đã trả lời ngày 29 tháng 5 năm 2011 lúc 11:14

Bcosynotbcosynot

from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))

5.3159 Huy hiệu vàng34 Huy hiệu bạc45 Huy hiệu Đồng

  

hello world

Mặc dù câu trả lời của tôi có thể không hữu ích bây giờ, tôi đang bỏ nó ở đây để hoạt động như một tài liệu tham khảo cho bất kỳ ai khác trong tương lai.May 12, 2013 at 9:01

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
4, thực sự, không in đẹp HTML được cung cấp bất chấp
from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
2.Jayesh Bhoot

Tuy nhiên, anh chị em của người Hồi giáo

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
6 -
from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
7 có hoạt động tốt.1 gold badge21 silver badges33 bronze badges

4

Vì vậy, người ta có thể sử dụng nó như sau:

from bs4 import BeautifulSoupprint(BeautifulSoup(html_string, 'html.parser').prettify())

Đầu ra là như thế này:Nov 13, 2017 at 7:26

Đã trả lời ngày 12 tháng 5 năm 2013 lúc 9:01Alex

Jayesh Bhootjayesh Bhoot6 gold badges61 silver badges71 bronze badges

1

1.4631 Huy hiệu vàng21 Huy hiệu bạc33 Huy hiệu đồng

Nếu bạn lưu trữ HTML dưới dạng chuỗi không được định dạng, trong một biến

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
8, nó có thể được thực hiện bằng cách sử dụng đẹp như sau:

pip install html5print

Đã trả lời ngày 13 tháng 11 năm 2017 lúc 7:26

html5-print ugly.html -o pretty.html

Alexalex

from html5print import HTMLBeautifierhtml = 'Page Title

Some text here

'print(HTMLBeautifier.beautify(html, 4))

10,7k6 huy hiệu vàng61 Huy hiệu bạc71 Huy hiệu đồngMar 19, 2018 at 20:49

Hướng dẫn python print html pretty - python in html khá đẹp

Nếu thêm một phụ thuộc nữa không phải là vấn đề, bạn có thể sử dụng gói HTML5Print. Ưu điểm so với các giải pháp khác, là nó cũng làm đẹp cả mã CSS và JavaScript được nhúng trong tài liệu HTML.pgmank

Để cài đặt nó, thực thi:5 gold badges34 silver badges47 bronze badges

1

Sau đó, bạn có thể sử dụng nó như một lệnh:

from yattag import indentrawhtml = "String with some HTML code..."result = indent( rawhtml, indentation = ' ', newline = 'rn', indent_text = True)print(result)

hoặc như mã python:May 6, 2018 at 8:02

Đã trả lời ngày 19 tháng 3 năm 2018 lúc 20:49Vadym Pasko

PGMANKPGMANK4 silver badges4 bronze badges

4.7655 Huy hiệu vàng34 Huy hiệu bạc47 Huy hiệu đồng

 xmlOutputBufferWriteString(buf, ">"); if ((format) && (!info->isinline) && (cur->next != NULL)) { if ((cur->next->type != HTML_TEXT_NODE) && (cur->next->type != HTML_ENTITY_REF_NODE) && (cur->parent != NULL) && (cur->parent->name != NULL) && (cur->parent->name[0] != 'p')) /* p, pre, param */ xmlOutputBufferWriteString(buf, "n"); } return;

Tôi đã thử cả hai giải pháp BeautifulSoup,

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
9 và HTML5Print, nhưng vì tôi đã sử dụng Yattag để tạo HTML nên có vẻ thích hợp hơn khi sử dụng chức năng
from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
1 của nó, tạo ra đầu ra được thụt đầu vào.followed by a sibling node (
from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
4) and isn’t one of
from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
5 then it will output a newline.

Đã trả lời ngày 6 tháng 5 năm 2018 lúc 8:02May 27, 2011 at 15:40

Vadym Paskovadym paskosamplebias

2414 Huy hiệu bạc4 Huy hiệu đồng6 gold badges103 silver badges102 bronze badges

Dưới mui xe,

from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
2 sử dụng
from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
3 để tuần tự hóa cây trở lại thành một chuỗi. Dưới đây là đoạn mã có liên quan xác định xem có nên nối thêm một dòng mới sau khi đóng thẻ:

Vì vậy, nếu một nút là một phần tử, không phải là một thẻ nội tuyến và được theo sau bởi một nút anh chị em (

from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
4) và là một trong những
from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
5 thì nó sẽ xuất ra một dòng mới.May 27, 2011 at 13:14

Đã trả lời ngày 27 tháng 5 năm 2011 lúc 15:40tsm

SampiasSamplebias2 gold badges20 silver badges35 bronze badges

3

36.2k6 Huy hiệu vàng103 Huy hiệu bạc102 Huy hiệu Đồng

Bạn có thể chỉ cần ống nó vào HTML TIDY không? Hoặc từ vỏ hoặc đến

from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
6.May 27, 2011 at 12:56

Đã trả lời ngày 27 tháng 5 năm 2011 lúc 13:14Boaz Yaniv

TSMTSM20 silver badges29 bronze badges

1

3,4482 Huy hiệu vàng20 Huy hiệu bạc35 Huy hiệu Đồng

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
0

Nếu bạn không quan tâm đến htmlness kỳ quặc (ví dụ: bạn phải hỗ trợ hoàn toàn hỗ trợ các ứng dụng khách Netscpae 2.0, vì vậy có

from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
7 thay vì
from lxml import etree, htmldocument_root = html.fromstring("

hello world

")print(etree.tostring(document_root, encoding='unicode', pretty_print=True))
8 là điều bắt buộc), bạn luôn có thể thay đổi phương pháp của mình thành XML XML, dường như công việc. Đây có lẽ là một lỗi trong LXML hoặc trong libxml, nhưng tôi không thể tìm thấy lý do cho nó.

from bs4 import BeautifulSoup as bsroot = lh.tostring(sliderRoot) #convert the generated HTML to a stringsoup = bs(root) #make BeautifulSoupprettyHTML = soup.prettify() #prettify the html
1

Đã trả lời ngày 27 tháng 5 năm 2011 lúc 12:56May 27, 2011 at 15:22

Boaz Yanivboaz Yanivsherpya

6.18620 Huy hiệu bạc29 Huy hiệu đồng2 gold badges32 silver badges49 bronze badges

0