Python writes a simple HTML page merge script _python

Source: Internet
Author: User

Recently wrote a bootstrap page ... Because the function needs to decide a page to solve all the problems, and then use jquery to dynamically display features .... However, the page would be quite large, with a stack of hidden modal windows and feature div all stacked together.

And then I wrote a small script with Python to support <include> tags, which is used to combine external HTML files to force a single large HTML page to be written.

Use the next feeling pretty good, share to everyone

How to use:

Use the <include src= "" > tag in HTML to import additional HTML code. Supports nested substitutions (such as a page nested b page, b page nesting c page). However, be careful to loop nesting (a page nested b page, b page nested a page), which causes a dead loop
The main page is index.html for the default processing page, and the merged page is generated newhtml.html
The specific code is as follows

Import codecs import webbrowser import sys charset = "Utf-8" #文件编码 files in the #读取text里的 <include> label and SRC attribute, replace the original label Def Repla
   Ceinclude (filename,text): Try:posa = Text.find ("<include") while posa!= -1:posc = Text.find (">", Posa) Tag = text[posa:posc+1] Posa = Text.find ("src=", posa) Posa + = 5 PosB = text.find ("\", posa) file = Text[posa:p OsB] #获取src中的文件名 print ("Processing:", file) tmpfile = Codecs.open (file, "R", CharSet) Tmptext = Tmpfile.read () tmptext = Replaceinclude (file,tmptext) #递归处理文件嵌套后的include标签 text = Text.replace (tag,tmptext) tmpfile.close () Posa = Text.f
 IND ("<include") return text; Except Exception as E:print ("Error: File", filename, "in", File, "Processing failed!") Error message: \ n ", E) sys.exit (1) readFile = Codecs.open (" index.html "," R ", CharSet) WriteFile = Codecs.open (" newhtml.html "," W " , charset) Try:text = Readfile.read () Text = Replaceinclude ("index.html", Text) writefile.write (text) Webbrowser.open (" Newhtml.html ") Finally:readFile.close () WRITefile.close () </pre>
 

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.