科技
如何使用htmlq提取html文件内容
2022-12-16 10:49  浏览:248

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 特别linuxprobe | htmlq --pretty '#content' | more

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

[root等localhost ~]# curl -s 特别linuxprobe | htmlq --attribute href a


人性化显示HTML:

[root等localhost ~]# curl --silent mgdm | htmlq --pretty '#posts'

帮助手册

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

[root等localhost ~]# htmlq --helphtmlq 0.3.0Michael Maclean <michael等mgdm>Runs CSS selectors on HTMLUSAGE: htmlq [FLAGS] [OPTIONS] [selector]...FLAGS: -B, --detect-base Try to detect the base URL from the <base> tag in the document. If not found, default to the 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 <attribute> only return this attribute (if present) from selected elements -b, --base <base> Use this URL as the base for links -f, --filename <FILE> The input file. Defaults to stdin -o, --output <FILE> The output file. Defaults to stdoutARGS: <selector>... The CSS expression to select [default: html]

总结

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