Mô-đun này định nghĩa một lớp dùng làm cơ sở để phân tích cú pháp các tệp văn bản được định dạng bằng HTML (Ngôn ngữ đánh dấu siêu văn bản) và XHTML
lớp html. trình phân tích cú pháp. HTMLParser(* , convert_charrefs=Đúng)Tạo một phiên bản trình phân tích cú pháp có thể phân tích cú pháp đánh dấu không hợp lệ
Nếu convert_charrefs là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 2 (mặc định), tất cả tham chiếu ký tự (ngoại trừ tham chiếu trong phần tử Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 3/Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 4) sẽ tự động được chuyển đổi thành ký tự Unicode tương ứng
Một phiên bản được cung cấp dữ liệu HTML và gọi các phương thức xử lý khi gặp thẻ bắt đầu, thẻ kết thúc, văn bản, nhận xét và các phần tử đánh dấu khác. Người dùng nên phân lớp và ghi đè các phương thức của nó để thực hiện hành vi mong muốn
Trình phân tích cú pháp này không kiểm tra xem thẻ kết thúc có khớp với thẻ bắt đầu hay không gọi trình xử lý thẻ kết thúc cho các phần tử được đóng hoàn toàn bằng cách đóng phần tử bên ngoài
Đã thay đổi trong phiên bản 3. 4. đã thêm đối số từ khóa convert_charrefs.
Đã thay đổi trong phiên bản 3. 5. Giá trị mặc định cho đối số convert_charrefs hiện là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 2.
Ứng dụng trình phân tích cú pháp HTML mẫu
Như một ví dụ cơ bản, dưới đây là một trình phân tích cú pháp HTML đơn giản sử dụng lớp để in ra các thẻ bắt đầu, thẻ kết thúc và dữ liệu khi chúng gặp phải
from html.parser import HTMLParser class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Encountered a start tag:", tag) def handle_endtag(self, tag): print("Encountered an end tag :", tag) def handle_data(self, data): print("Encountered some data :", data) parser = MyHTMLParser() parser.feed('<html><head><title>Test</title></head>' '<body><h1>Parse me!</h1></body></html>')
Đầu ra sau đó sẽ là
Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html
phương pháp
trường hợp có các phương pháp sau
HTMLParser. nguồn cấp dữ liệu(dữ liệu)Cung cấp một số văn bản cho trình phân tích cú pháp. Nó được xử lý trong chừng mực nó bao gồm các yếu tố hoàn chỉnh; . dữ liệu phải được
HTMLParser. đóng()Buộc xử lý tất cả dữ liệu được lưu vào bộ đệm như thể nó được theo sau bởi dấu cuối tệp. Phương thức này có thể được định nghĩa lại bởi một lớp dẫn xuất để xác định quá trình xử lý bổ sung ở cuối đầu vào, nhưng phiên bản được xác định lại phải luôn gọi phương thức của lớp cơ sở
HTMLParser. đặt lại()Đặt lại phiên bản. Mất tất cả dữ liệu chưa xử lý. Điều này được gọi ngầm tại thời điểm khởi tạo
HTMLParser. nhận vị trí()Trả về số dòng hiện tại và phần bù
HTMLParser. get_starttag_text()Trả lại văn bản của thẻ bắt đầu được mở gần đây nhất. Điều này thường không cần thiết cho quá trình xử lý có cấu trúc, nhưng có thể hữu ích trong việc xử lý HTML “khi được triển khai” hoặc để tạo lại đầu vào với những thay đổi tối thiểu (có thể giữ nguyên khoảng trắng giữa các thuộc tính, v.v. )
Các phương thức sau đây được gọi khi gặp dữ liệu hoặc các phần tử đánh dấu và chúng có nghĩa là sẽ bị ghi đè trong một lớp con. Việc triển khai lớp cơ sở không làm gì cả (ngoại trừ)
HTMLParser. handle_starttag(tag , attrs)Phương thức này được gọi để xử lý thẻ bắt đầu của một phần tử (e. g. from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Start tag:", tag) for attr in attrs: print(" attr:", attr) def handle_endtag(self, tag): print("End tag :", tag) def handle_data(self, data): print("Data :", data) def handle_comment(self, data): print("Comment :", data) def handle_entityref(self, name): c = chr(name2codepoint[name]) print("Named ent:", c) def handle_charref(self, name): if name.startswith('x'): c = chr(int(name[1:], 16)) else: c = chr(int(name)) print("Num ent :", c) def handle_decl(self, data): print("Decl :", data) parser = MyHTMLParser() 6)
Đối số thẻ là tên của thẻ được chuyển thành chữ thường. Đối số attrs là danh sách gồm các cặp from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Start tag:", tag) for attr in attrs: print(" attr:", attr) def handle_endtag(self, tag): print("End tag :", tag) def handle_data(self, data): print("Data :", data) def handle_comment(self, data): print("Comment :", data) def handle_entityref(self, name): c = chr(name2codepoint[name]) print("Named ent:", c) def handle_charref(self, name): if name.startswith('x'): c = chr(int(name[1:], 16)) else: c = chr(int(name)) print("Num ent :", c) def handle_decl(self, data): print("Decl :", data) parser = MyHTMLParser() 7 chứa các thuộc tính được tìm thấy bên trong dấu ngoặc _______68 của thẻ. Tên sẽ được dịch sang chữ thường và dấu ngoặc kép trong giá trị đã bị xóa, đồng thời tham chiếu ký tự và thực thể đã được thay thế
Chẳng hạn, đối với thẻ from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Start tag:", tag) for attr in attrs: print(" attr:", attr) def handle_endtag(self, tag): print("End tag :", tag) def handle_data(self, data): print("Data :", data) def handle_comment(self, data): print("Comment :", data) def handle_entityref(self, name): c = chr(name2codepoint[name]) print("Named ent:", c) def handle_charref(self, name): if name.startswith('x'): c = chr(int(name[1:], 16)) else: c = chr(int(name)) print("Num ent :", c) def handle_decl(self, data): print("Decl :", data) parser = MyHTMLParser() 9, phương thức này sẽ được gọi là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 00
Tất cả các tham chiếu thực thể từ được thay thế trong các giá trị thuộc tính
HTMLParser. handle_endtag(thẻ)Phương thức này được gọi để xử lý thẻ kết thúc của một phần tử (e. g. Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 02)
Đối số thẻ là tên của thẻ được chuyển thành chữ thường
HTMLParser. handle_startendtag(tag , attrs)Tương tự như , nhưng được gọi khi trình phân tích cú pháp gặp thẻ trống kiểu XHTML (Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 04). Phương thức này có thể bị ghi đè bởi các lớp con yêu cầu thông tin từ vựng cụ thể này;
HTMLParser. handle_data(dữ liệu)Phương thức này được gọi để xử lý dữ liệu tùy ý (e. g. các nút văn bản và nội dung của Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 07 và Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 08)
HTMLParser. handle_entityref(tên)Phương thức này được gọi để xử lý một tham chiếu ký tự được đặt tên có dạng Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 09 (e. g. Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 60), trong đó tên là tham chiếu thực thể chung (e. g. Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 61). Phương thức này không bao giờ được gọi nếu convert_charrefs là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 2
HTMLParser. handle_charref(tên)Phương thức này được gọi để xử lý các tham chiếu ký tự số thập phân và thập lục phân có dạng Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 63 và Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 64. Ví dụ: số thập phân tương đương với Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 60 là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 66, trong khi số thập lục phân là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 67; . Phương thức này không bao giờ được gọi nếu convert_charrefs là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 2
HTMLParser. handle_comment(dữ liệu)Phương thức này được gọi khi gặp một bình luận (e. g. Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 71)
Ví dụ: nhận xét Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 72 sẽ khiến phương thức này được gọi với đối số Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 73
Nội dung của các nhận xét có điều kiện (condcom) của Internet Explorer cũng sẽ được gửi đến phương thức này, vì vậy, đối với Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 74, phương thức này sẽ nhận được Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 75
HTMLParser. handle_decl(decl)Phương thức này được gọi để xử lý một khai báo doctype HTML (e. g. Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 76)
Tham số boong sẽ là toàn bộ nội dung khai báo bên trong đánh dấu ________ 277 (e. g. Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 78)
HTMLParser. handle_pi(dữ liệu)Phương thức được gọi khi gặp hướng dẫn xử lý. Tham số dữ liệu sẽ chứa toàn bộ hướng dẫn xử lý. Ví dụ: đối với hướng dẫn xử lý Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 79, phương thức này sẽ được gọi là Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 60. Nó dự định sẽ bị ghi đè bởi một lớp dẫn xuất;
Ghi chú
Lớp sử dụng các quy tắc cú pháp SGML để xử lý các hướng dẫn. Hướng dẫn xử lý XHTML sử dụng Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 62 ở cuối sẽ làm cho Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 62 được đưa vào dữ liệu
HTMLParser. unknown_decl(dữ liệu)Phương thức này được gọi khi trình phân tích cú pháp đọc một khai báo không được nhận dạng
Tham số dữ liệu sẽ là toàn bộ nội dung khai báo bên trong đánh dấu Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 64. Đôi khi nó hữu ích khi bị ghi đè bởi một lớp dẫn xuất. Việc triển khai lớp cơ sở không làm gì cả
ví dụ
Lớp sau triển khai trình phân tích cú pháp sẽ được sử dụng để minh họa thêm các ví dụ
from html.parser import HTMLParser from html.entities import name2codepoint class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Start tag:", tag) for attr in attrs: print(" attr:", attr) def handle_endtag(self, tag): print("End tag :", tag) def handle_data(self, data): print("Data :", data) def handle_comment(self, data): print("Comment :", data) def handle_entityref(self, name): c = chr(name2codepoint[name]) print("Named ent:", c) def handle_charref(self, name): if name.startswith('x'): c = chr(int(name[1:], 16)) else: c = chr(int(name)) print("Num ent :", c) def handle_decl(self, data): print("Decl :", data) parser = MyHTMLParser()
Phân tích cú pháp một loại tài liệu
Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 0
Phân tích cú pháp một phần tử với một vài thuộc tính và tiêu đề
Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 6
Nội dung của các phần tử Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 3 và Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 4 được trả về nguyên trạng mà không cần phân tích cú pháp thêm
Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 7
Phân tích cú pháp nhận xét
Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 6
Phân tích các tham chiếu ký tự có tên và số và chuyển đổi chúng thành ký tự chính xác (lưu ý. 3 tài liệu tham khảo này đều tương đương với Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 67)
from html.parser import HTMLParser class MyHTMLParser(HTMLParser): def handle_starttag(self, tag, attrs): print("Encountered a start tag:", tag) def handle_endtag(self, tag): print("Encountered an end tag :", tag) def handle_data(self, data): print("Encountered some data :", data) parser = MyHTMLParser() parser.feed('<html><head><title>Test</title></head>' '<body><h1>Parse me!</h1></body></html>') 8
Cung cấp các khối không đầy đủ để hoạt động, nhưng có thể được gọi nhiều lần (trừ khi convert_charrefs được đặt thành Encountered a start tag: html Encountered a start tag: head Encountered a start tag: title Encountered some data : Test Encountered an end tag : title Encountered an end tag : head Encountered a start tag: body Encountered a start tag: h1 Encountered some data : Parse me! Encountered an end tag : h1 Encountered an end tag : body Encountered an end tag : html 2)