当前位置:首页 > 日记本 > 正文内容

python xpath语法总结

zhangchap3年前 (2021-05-20)日记本244

python xpath语法总结:

常用的:

//

1.从任意节点开始

/

2.从根节点开始

//div/p

3.div下的p标签

//div[@class="hrzz_bottom"]/ul/li/a/@href

//div[@class="hrzz_bottom"]/ul/li/a/attribute::href

3.1 attribute 获取所有属性的值

//div[@class="hrzz_bottom"]/ul/li/a/attribute::href

4.1.li下的ahref属性即链接

//div[@class="hrzz_bottom"]//li/a/@title

4.2.li下的a链接title属性

//a[contains(@href,'tags.php')]/@href

4.3.所有包含tags.php的a链接

//p/text()

5.获取p标签下的文字

//div[@class="hrzz_bottom"]

6.class值为hrzz_bottom的div标签

//div[contains(@class,"hrzz_bottom")]

7.class值包含hrzz_bottom的div标签

//div[contains(@class, "show_content")]|//div[contains(@class,"hl_body")]

8.class值包含hrzz_bottom的div或者class值包含hl_body的div

//div[starts-with(@class,"show_")]

8.1 div class值开头包含show_

//div/p[3]

9.div下的第三个p标签

//div/p[position()>3]

10.从div下的第四个节点开始选

//div/p[last()]

10.1div下的最后一个p标签

//div/p/text()

11. 获取接点的文本


实例化对象

from lxml import etree
doc = etree.HTML(html)
title = doc.xpath('//div')
for tr in trs:
    href=tr.xpath(".//a")

注意在trs中寻找a标签,需要在//前面加一个.,否则就会在整个html中寻找a标签


标签: python笔记
分享给朋友:

相关文章

python jieba分词

import jieba from jieba.analyse import tfidf words = jieba.lcut('...

json输出json.dumps中文为ascii编码如何解决?

import json print json.dumps('中国') 输出:"\u4e2d\u56fd" json.dumps(...

python jieba分词自定义分词器及自定义词典

import jieba jieba.initialize() # 自定义分词器的写法 n_c = jieba.Tokenizer(dictionary=...

python判断当前系统为win还是Linux

使用sys模块进行判断,以便启用在win平台下不支持的代码:import sys p = sys.platform if p != &#...

python 字典排序

prefix_word = {'怎么': 4, '昨晚': 27, '会': 56...

python下random随机选择的三种方式

from random import sample,choice,choices list_1 = [1,2,3,4,5,6] # 从列...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。