Understanding of XPath crawling Web data

Source: Internet
Author: User
Tags xpath

First, install the XPath configuration

Install the XPath installation lxml library to download from a Python third-party library, or pip install lxml

Learn to use XPath

Import etree Module

From lxml import etree

Extracting web page code of interest using XPath

Selector= etree. HTML (HTML) (HTML page source code, via HTML = Requests.get (...). Text gets)

Converts the acquired source code into text that can be extracted with XPath

Content = Selector.xpath (a magical symbol)

This symbol can be obtained by right-clicking the →copy Xpath in the review element.

Such as


Xpath matches the idea:

Based on the structure of the HTML:

1. Tree-like structure

2, layer by step expansion

3. Level-by-layer positioning

4. Finding independent nodes

To extract content using XPath:

Locating the root node//

Looking down layer/

Extract text content/text ()

Extract attribute Contents/@xxxx

As an example:

Such as


Another special use of XPath--start with the same character


As an example,


Extract the labels that begin with test


Another special use – label set label


As an example,

A string (.) was used, with two back XPath




to learn the XPath URL: Http://search.jikexueyuan.com/course/?q=Python%E7%88%AC%E8%99%AB

Understanding of crawling Web data for XPath

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.