请求数据分析 xpath语法 与lxml库

前情提要:

  上节学过从网上获取请求,获取返回内容,带理

获取内容之后,第二部就是获取请求的数据分析

  一:xpath 语法

 浏览器一般会自带xpatn 解析

这里大概讲述一下xpath 的基本操作

二:式例

我用的是360 浏览器...(..用了好多年了..习惯了..)

我们拿笔趣阁进行测试

https://www.biquge5200.cc/46_46254/

1>进入开发者模式

2>写xpath 

 >2.1 

  //        获取子孙节点

    div        div  节点

  //          获取上一层下的子孙节点

  dl          dl  节点

  //            获取上一层下的子孙节点

  dd         dd节点

  [position()]             节点内的位置

  >                            比较运算符

  9                             第九个节点

三: lxml 解析器

# 本地使用
# from lxml import etree
# htmlElent =etree.HTML(text)
# print(type(htmlElent))  #html对象
# print(etree.tostring(htmlElent,encoding='utf-8').decode('utf-8'))

# etree.tostring 规范化并补全


#读取文件中的html
# from lxml import etree
# parser =etree.HTMLParser(encodeing=('utf-8'))  #定义解析器
# #解析器作用 补全html 和规范html
# htmlElent =etree.parse("lagou.html",parser=parser)
# # 读取本地html 文件. 传入解析器
# print(etree.tostring(htmlElent,encoding='utf-8').decode('utf-8'))
原文地址:https://www.cnblogs.com/baili-luoyun/p/10426842.html