如何使用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 数据。

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
【NI Multisim 14...   目录 序言 一、工具栏 🍊1.“标准”工具栏 🍊 2.视图工具...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
月入8000+的steam搬砖... 大家好,我是阿阳 今天要给大家介绍的是 steam 游戏搬砖项目,目前...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...