Python Forum
XML parsing and generating HTML page Python 3.6
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
XML parsing and generating HTML page Python 3.6
#1
is it possible to parse the XML file and generate the html page in python 3.6
i want extract the data from XML and put it needed info into html page
Reply
#2
Answer is yes, using lxml (or BeautifulSoup):

this isn't the cleanest code in town, but it uses lxml to parse XML
example: https://www.scrapehero.com/how-to-scrape...-and-lxml/
Reply
#3
(Aug-24-2018, 05:06 AM)Madhuri Wrote: is it possible to parse the XML file and generate the html page in python 3.6
i want extract the data from XML and put it needed info into html page
Yes it possible,it depend of the HTML is already generated and have a server running.

Here a example that parese XML and generate HTML using jinja2.
I use jinja2 with Flask(jinja2 is build in) for sending stuff from server to HTML.
jinja2 can also work alone as shown here.
from bs4 import BeautifulSoup
from jinja2 import Environment, FileSystemLoader

xml ='''\
<?xml version="1.0" encoding="UTF-8"?>
<note>
  <to>Tove</to>
  <from>Jani</from>
  <heading>Reminder</heading>
  <body>Don't forget me this weekend!</body>
</note>'''

# parse
soup = BeautifulSoup(xml, 'xml')
mes_from = soup.find('from').text

# Generate html
# test.html
#<h1>{{ message }}</h1>
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('test.html')
output = template.render(message=mes_from)
print(output)
Output:
<h1>Jani</h1> # If remove .text from parsing <h1><from>Jani</from></h1>
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,536 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Parsing html page and working with checkbox (on a captcha) straannick 17 11,066 Feb-04-2021, 02:54 PM
Last Post: snippsat
  Saving html page and reloading into selenium while developing all xpaths Larz60+ 4 4,105 Feb-04-2021, 07:01 AM
Last Post: jonathanwhite1
  API auto-refresh on HTML page using Flask toc 2 11,748 Dec-23-2020, 02:00 PM
Last Post: toc
  Selenium Parsing (unable to Parse page after loading) oneclick 7 5,892 Oct-30-2020, 08:13 PM
Last Post: tomalex
  Help: Beautiful Soup - Parsing HTML table ironfelix717 2 2,623 Oct-01-2020, 02:19 PM
Last Post: snippsat
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,329 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  use Xpath in Python :: libxml2 for a page-to-page skip-setting apollo 2 3,583 Mar-19-2020, 06:13 PM
Last Post: apollo
  Help on parsing simple text on HTML amaumox 5 3,399 Jan-03-2020, 05:50 PM
Last Post: amaumox
  Problem parsing website html file thefpgarace 2 3,169 May-01-2018, 11:09 AM
Last Post: Standard_user

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020