• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
百里丶落云
Gee,gee,baby,baby
            管理     
请求数据分析 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'))

 

年与时驰,意与日去,遂成枯落, 多不接世,悲守穷庐,将复何及。
posted on 2019-02-24 17:06  百里丶落云  阅读(249)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3