htmlq能夠?qū)?HTML 數(shù)據(jù)進(jìn)行 sed 或 grep 操作霉旗。我們可以使用 htmlq 搜索扯夭、切片和過濾 HTML 數(shù)據(jù)缓醋。讓我們看看如何在?Linux?或 Unix 上安裝和使用這個(gè)方便的工具并處理 HTML 數(shù)據(jù)该默。
什么是htmlq住练?
htmlq類似于?jq地啰,但用于 HTML。使用 CSS 選擇器從 HTML 文件中提取部分內(nèi)容讲逛。在 CSS 中亏吝,選擇器用于定位我們想要設(shè)置樣式的網(wǎng)頁上的 HTML 元素。例如盏混,我們可以使用此工具輕松提取圖像或其他 URL蔚鸥。
安裝htmlq
首先需要在系統(tǒng)中安裝cargo然后使用cargo來安裝htmlq:
[root@localhost ~]# yum -y install cargo
[root@localhost ~]# cargo install htmlq
設(shè)置可執(zhí)行的路徑
確保將?$HOME/.cargo/bin?添加到 PATH 變量中惜论,以便能夠使用 export?命令運(yùn)行已安裝的二進(jìn)制文件:
[root@localhost ~]# echo 'export PATH="$PATH:$HOME/.cargo/bin"' >> ~/.bash_profile
[root@localhost ~]# . ~/.bash_profile
如何使用 htmlq 從 HTML 文件中提取內(nèi)容?
下面是使用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'
幫助手冊(cè)
使用下面命令查看幫助頁面:
[root@localhost ~]# htmlq --help
htmlq 0.3.0
Michael Maclean <michael@mgdm.net>
Runs CSS selectors on HTML
USAGE:
? ? 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 information
OPTIONS:
? ? -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 stdout
ARGS:
? ? <selector>...? ? The CSS expression to select [default: html]
總結(jié)
htmlq能夠?qū)?HTML 數(shù)據(jù)進(jìn)行 sed 或 grep 操作来涨。我們可以使用 htmlq 搜索、切片和過濾 HTML 數(shù)據(jù)启盛。