Parsing HTML with XPath and xml.dom in Python

Source: Internet
Author: User
Tags xpath

This recommended combination is Xml.dom.minidom and XPath. Where Xml.dom.minidom is the standard library for Python, no installation is required. XPath is an open source project Py-dom-xpath by Google.

Install Py-dom-xpath:

    1. Download the compressed package from https://py-dom-xpath.googlecode.com/files/py-dom-xpath-0.1.tar.gz, unzip it.
    2. CD to py-dom-xpath-0.1 directory
    3. Execute command: C:\python27\python setup.py install
    4. After the command executes, this directory has two more folders, build and dist, and copies the XPath directory under the/build/lib/folder to the C:\Python27\Lib directory, which is the installation complete.

The test HTML file, simple.html, reads as follows:

  

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <title>A simple HTML file</title>5 <Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"/>6 </Head>7 <Body>8 <Div>9     <Div>A</Div>Ten     <Div>B</Div> One     <Div>C</Div> A </Div> - </Body> - </HTML>

Python file:

  

 1  import   XPath  2  import   Xml.dom.minidom  3  4  xml = Xml.dom.minidom.parse ( '  

A notable problem is that the first line of simple.html, the document header <! The ' DOCTYPE ' in DOCTYPE html> must be capitalized, otherwise xml.dom.minidom parsing will fail. In the next article, we'll cover another library that parses HTML, and there's no such problem.

Parsing HTML with XPath and xml.dom in Python

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.