X Path lxml用法簡介

2021-10-06 08:52:33 字數 721 閱讀 1792

x-path 與正規表示式都是對網頁資料的提取,正規表示式功能強大,但相對複雜,x-path語法相對簡單,可以通過谷歌外掛程式安裝x-path

表示式描述

/根節點擊取或下級

//任意節點,不考慮位置

.當前節點

…當前節點的父節點

@選取屬性

*匹配任何節點

根據節點篩選

contains(@屬性,「包含的內容」

模糊查詢

text()

文字內容

取得class為classify的h3標籤下a標籤內的內容

//h3[@class="classify"]/a/text()

利用模糊查詢查詢class類名中包含classify_list的所有div標籤下的span下的a標籤的文字內容

//div[contains(@class,"classify_list")]/span/a/text()

import lxml.etree as le
with open('123.html','r',encoding='utf-8') as f:

html = f.read()

html_x = le.html(html)

div_x_s=html_x.xpath('//div[@class])

nth element 用法簡介

簡單的說nth element演算法僅排序第nth個元素 從0開始的索引 如iarray first,last 元素區間 排序後 iarray nth 就是第nth大的元素 從0開始 要注意的是 first,nth nth,last 內 的大小循序還不一定 只能確定iarray nth 是第nth大...

C STL 用法簡介

感覺類似python的字典 需要 include 定義是map m 如 map mapstudent int 是 key,string是value 是乙個對映關係,可以使用key通過下標訪問,訪問其中的元素。定義了三種型別,插入鍵值對的插入規則 第一種就是直接通過下標插入 mapstudent ke...

ToStringBuilder用法簡介

1 tostringbuilder hashcodebuilder equalsbuilder tostringstyle reflectiontostringbuilder comparetobuilder等這些類都是位於apace的commons lang.jar下面的,所以要使用這些類一定要匯...