如何使用htmlq提取html文件内容
创始人
2024-03-25 16:08:43
0
htmlq能够对 HTML 数据进行 sed 或 grep 操作。我们可以使用 htmlq 搜索、切片和过滤 HTML 数据。让我们看看如何在 Linux 或 Unix 上安装和使用这个方便的工具并处理 HTML 数据。

什么是htmlq?

htmlq类似于 jq,但用于 HTML。使用 CSS 选择器从 HTML 文件中提取部分内容。在 CSS 中,选择器用于定位我们想要设置样式的网页上的 HTML 元素。例如,我们可以使用此工具轻松提取图像或其他 URL。

安装htmlq

首先需要在系统中安装cargo然后使用cargo来安装htmlq:

[root@localhost ~]# yum -y install cargo
[root@localhost ~]# cargo install htmlq

设置可执行的路径

确保将 $HOME/.cargo/bin 添加到 PATH 变量中,以便能够使用 export 命令运行已安装的二进制文件:

[root@localhost ~]# echo 'export PATH="$PATH:$HOME/.cargo/bin"' >> ~/.bash_profile 
[root@localhost ~]# . ~/.bash_profile 

如何使用 htmlq 从 HTML 文件中提取内容?

下面是使用curl和htmlq的用法:

curl -s url | htmlq '#css-selector'
curl -s url2 | htmlq '.css-selector'
curl -s https://www.linuxprobe.com | htmlq --pretty '#content' | more

让我们找到页面中的所有链接。例如:

[root@localhost ~]# curl -s https://www.linuxprobe.com | htmlq --attribute href a


人性化显示HTML:

[root@localhost ~]# curl --silent https://mgdm.net | htmlq --pretty '#posts'

帮助手册

使用下面命令查看帮助页面:

[root@localhost ~]# htmlq --help
htmlq 0.3.0
Michael Maclean 
Runs CSS selectors on HTMLUSAGE:htmlq [FLAGS] [OPTIONS] [selector]...FLAGS:-B, --detect-base          Try to detect the base URL from the  tag in the document. If not found, default tothe value of --base, if supplied-h, --help                 Prints help information-w, --ignore-whitespace    When printing text nodes, ignore those that consist entirely of whitespace-p, --pretty               Pretty-print the serialised output-t, --text                 Output only the contents of text nodes inside selected elements-V, --version              Prints version informationOPTIONS:-a, --attribute     Only return this attribute (if present) from selected elements-b, --base               Use this URL as the base for links-f, --filename           The input file. Defaults to stdin-o, --output             The output file. Defaults to stdoutARGS:...    The CSS expression to select [default: html]

总结

htmlq能够对 HTML 数据进行 sed 或 grep 操作。我们可以使用 htmlq 搜索、切片和过滤 HTML 数据。

相关内容

热门资讯

前端-session、jwt 目录:   (1)session (2&#x...
linux入门---制作进度条 了解缓冲区 我们首先来看看下面的操作: 我们首先创建了一个文件并在这个文件里面添加了...
关于测试,我发现了哪些新大陆 关于测试 平常也只是听说过一些关于测试的术语,但并没有使用过测试工具。偶然看到编程老师...
前缀和与对数器与二分法 1. 前缀和 假设有一个数组,我们想大量频繁的去访问L到R这个区间的和,...
nodejs:本地安装nvm实... 一、背景-使用不同版本node的原因 vue3+ts、nuxt3版本,node...
JAVA集合知识整理 Java集合知识整理 HashMap相关 HashMap的底层数据结构:jdk1.8之...
无刷直流电机介绍及单片机控制实... 无刷直流电机介绍及单片机控制实例前言基本概念优势与劣势使用寿命基本结构使用单片机控制实例电子调速器&...
fwdiary(2) dp2 1.传纸条  AcWing 275. 传纸条 - AcWing 走两条路,走一条最大的...
常用的DOS命令 常用的DOS命令 DOS(Disk Operating System,磁...
<C++> 类和对象(下) 1.const成员函数将const修饰的“成员函数”称之为const成员函数,cons...