良许Linux教程网 干货合集 通过htmlq提取html文件内容

通过htmlq提取html文件内容

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

如何使用htmlq提取html文件内容如何使用htmlq提取html文件内容 人性化显示HTML:

[root@localhost ~]# curl --silent https://mgdm.net | htmlq --pretty '#posts'
如何使用htmlq提取html文件内容如何使用htmlq提取html文件内容

帮助手册

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

[root@localhost ~]# htmlq --help
htmlq 0.3.0
Michael Maclean 
Runs CSS selectors on HTML

USAGE:
   htmlq [FLAGS] [OPTIONS] [selector]...

FLAGS:
   -B, --detect-base          Try to detect the base URL from the  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 information

OPTIONS:
   -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 stdout

ARGS:
   ...    The CSS expression to select [default: html]
如何使用htmlq提取html文件内容如何使用htmlq提取html文件内容

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

137e00002230ad9f26e78-265x300
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部