TAILIEUCHUNG - Dive Into Python-Chapter 8. HTML Processing

Tham khảo tài liệu 'dive into python-chapter 8. html processing', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Chapter 8. HTML Processing . Diving in I often see questions on like How can I list all the headers images links in my HTML document How do I parse translate munge the text of my HTML document but leave the tags alone How can I add remove quote attributes of all my HTML tags at once This chapter will answer all of these questions. Here is a complete working Python program in two parts. The first part is a generic tool to help you process HTML files by walking through the tags and text blocks. The second part is an example of how to use to translate the text of an HTML document but leave the tags alone. Read the doc strings and comments to get an overview of what s going on. Most of it will seem like black magic because it s not obvious how any of these class methods ever get called. Don t worry all will be revealed in due time. Example . If you have not already done so you can download this and other examples used in this book. from sgmllib import SGMLParser import htmlentitydefs class BaseHTMLProcessor SGMLParser def reset self extend called by self def unknown_starttag self tag attrs called for each start tag attrs is a list of attr value tuples . for pre class- screen tag pre attrs class screen Ideally we would like to reconstruct original tag and attributes but we may end up quoting attribute values that weren t quoted in the source document or we may change the type of quotes around the attribute value single to double quotes . Note that improperly embedded non-HTML code like client-side Javascript may be parsed incorrectly by the ancestor causing runtime script errors. All non-HTML code must be enclosed in HTML comment tags --code -- to ensure that it will pass through this parser unaltered in handle_comment . strattrs join s s key value for key value in attrs tag s strattrs s locals def .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.