Basic use of XPath for Python crawlers

Source: Internet
Author: User

First, Introduction

XPath is a language that looks for information in an XML document. XPath can be used to traverse elements and attributes in an XML document. XPath is the main element of the XSLT standard, and both XQuery and XPointer are built on top of the XPath expression.

Reference

Second, installation

PIP3 Install lxml

 

Third, the use

  1. Import

From lxml import etree

2. Basic use

From lxml Import etreewb_data = "" "        <div>            <ul>                 <li class=" item-0 "><a href=" Link1.html ">first item</a></li>                 <li class=" item-1 "><a href=" link2.html ">second Item </a></li>                 <li class= "item-inactive" ><a href= "link3.html" >third item</a></li >                 <li class= "item-1" ><a href= "link4.html" >fourth item</a></li>                 <li class= " item-0 "><a href=" link5.html ">fifth item</a>             </ul> </div>" ""        "html = etree. HTML (wb_data) print (HTML) result = etree.tostring (HTML) print (Result.decode ("Utf-8"))

From the results below, our printer HTML is actually a Python object, etree.tostring (HTML) is not the whole HTML of the basic wording, complete the lack of arms and legs of the label.

<element html at 0x39e58f0>

3, to obtain the content of a tag (basic use), note, get all the contents of a tag, a will not have to add a forward slash, otherwise error.

The wording of a

html = etree. HTML (wb_data) Html_data = Html.xpath ('/html/body/div/ul/li/a ') print (HTML) for I in Html_data:    print (I.text) < Element html at 0x12fe4b8>first itemsecond itemthird Itemfourth itemfifth Item

Two (just add a/text () directly after the label that needs to find the content)

html = etree. HTML (wb_data) Html_data = Html.xpath ('/html/body/div/ul/li/a/text () ') print (HTML) for I in Html_data:    print (i) < Element html at 0x138e4b8>first itemsecond itemthird Itemfourth itemfifth Item

  

Basic use of XPath for Python crawlers

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.