当前位置:网站首页>一晚上做了一个xpath终结者:xpath-helper-plus
一晚上做了一个xpath终结者:xpath-helper-plus
2022-06-25 18:19:00 【潘森迷】
前言
作为一个资深『xpath』提取工程师,想要快速从页面中拿到数据,肯定需要借助一些工具,
而最初接触的就是xpath-helper这块浏览器插件。使用一段时间后,发现笔者有一些特别的需求,想在此基础上扩展一下。
于是乎就学习了如何开发chrome插件?如何使用自己属性的vue来开发?经过双休一顿文档、gayhub了解之后,有了这样一款工具。
问题
xpath-helper2.0.2原生是支持按住shift后通过鼠标来定位选择元素,并输出xpath语法,但是这种方式出来的xptah语法并不友好。
比如说我想拾取掘金的某个文章标题:
/html/body/div[@id=‘__nuxt’]/div[@id=‘__layout’]/div[@id=‘juejin’]/div[@class=‘view-container container’]/main[@class=‘container main-container with-view-nav’]/div[@class=‘view timeline-index-view’]/div[@class=‘timeline-container’]/div[@class=‘timeline-content’]/aside[@class=‘index-aside aside’]/div[@class=‘signin-tip signin’]/div[@class=‘first-line’]/button[@class=‘btn signin-btn’]/span[@class=‘btn-text’]
它出来了一个xpath语法,这个xpath语法是从DOM根节点开始逐一向下(源码是从下至上)来拼接的。这样就会变成十分地冗余。
虽然它的确能够精准定位到我们想要的元素,但是一旦把这样的xpath语法复制到代码里,是十分可怕的。
当然还有一部分程序员热衷于选择使用chrome原生的元素复制成xpath。

出来的结果是:
//*[@id=“juejin”]/div[1]/main/div/div/div/aside/div[1]/div[1]/button/span
显然以上两种方案都有其问题:语法冗余、毫无可读性。
解决
笔者给出的解决方案是在xpath-helper基础上,增加一个辅助功能,它可以最大化精简xpath语法,一旦发现其语法能够识别到该元素就不再继续往上查找。而是立刻返回。

//span[@class=‘btn-text’]
并且它也可以友好的转为css选择器语法:
span.btn-text
我们可以在chrome的元素中检查这个语法:
- xpath:

- css:

实现
xpath-helper-plus其核心API均来自于xpath-helper。
笔者在此基础上增加了一些额外的功能,比如:精简xpath语句、转化CSS选择器。
与原有的xpath helper不同的是,这次chrome插件采用Vue3+vite来开发,面向组件,通过vite打包成chrome插件规范的文件目录结构。
未来可能会提供更多的功能。或者在此基础上开发其他插件。
开源
边栏推荐
- Some recursive and iterative problem solving ideas of binary tree (clear and easy to understand)
- [in depth understanding of tcapulusdb technology] tcapulusdb model
- Skills to master in advanced development
- [in depth understanding of tcapulusdb technology] tcapulusdb operation and maintenance doc
- GNU nano
- Training of long and difficult sentences in postgraduate entrance examination English Day82
- [deeply understand tcapulusdb technology] tcapulusdb import data
- IVX sailing
- [deeply understand tcapulusdb technology] tmonitor system upgrade
- New typereference usage fastjson[easy to understand]
猜你喜欢

Use pagoda to set up mqtt server

【深入理解TcaplusDB技术】 Tmonitor模块架构
![[deeply understand tcapulusdb technology] cluster management operation](/img/5f/f501f557171d438f7cc9cc4427f234.png)
[deeply understand tcapulusdb technology] cluster management operation

IVX 启航

Part 5:vs2017 build qt5.9.9 development environment

05 virtual machine stack

Boiled peanuts
![[deeply understand tcapulusdb technology] tmonitor system upgrade](/img/7b/8c4f1549054ee8c0184495d9e8e378.png)
[deeply understand tcapulusdb technology] tmonitor system upgrade

Handling method of qstring containing "\u0000" in QT

跨境电商亚马逊、eBay、Shopee、Lazada、速卖通、沃尔玛、阿里国际等平台,怎样进行自养号测评更安全?
随机推荐
Addition, deletion, modification and query of mysql~ tables (detailed and easy to understand)
RMAN backup database_ Restart RMAN backup
What is the ranking of the top ten securities companies? Is it safe to open a mobile account?
Anaconda download Tsinghua source
Basic operations and basic data types of MySQL database
【深入理解TcaplusDB技术】TcaplusDB新增机型
1. Understanding of norm
RMAN备份数据库_使用RMAN做拆分镜像(split mirror)备份
06 local method interface
GNU nano
Redis configuration (Internet access, password)
Redis trend - NVM memory
踩坑记录---线程池拒绝策略引起的一系列巧合
LeetCode力扣(剑指offer 26-30)26. 树的子结构 27. 二叉树的镜像 28. 对称的二叉树 29. 顺时针打印矩阵 30. 包含min函数的栈
Matlab中图形对象属性gca使用
158_模型_Power BI 使用 DAX + SVG 打通制作商业图表几乎所有可能
[in depth understanding of tcapulusdb technology] how to realize single machine installation of tmonitor
【深入理解TcaplusDB技术】单据受理之事务执行
[in depth understanding of tcapulusdb technology] new models of tcapulusdb
connect to address IP: No route to host