儀表我們大多數人都使用愛並使用 jq 命令。 它在 Linux 或類 Unix 系統上運行,並從 JSON 文檔中提取數據。最近發現 htmlq 和 jq 一樣,是用 Rustlang 編寫的。 想像一下能夠 sed 或 grep HTML 數據。 您可以使用 htmlq 來搜索、切片和過濾 HTML 數據。讓我們看看如何在 Linux 或 Unix 上安裝和使用這個方便的工具來處理 HTML 數據。
什麼是 htmlq 工具?
類似於 jq,但用於 HTML。 使用 CSS 選擇器從 HTML 文件中提取部分內容。 CSS 使用選擇器在您想要設置樣式的網頁上搜索 HTML 元素。例如,您可以使用此工具輕鬆提取圖像和其他 URL。
在 Linux 或 Unix 上安裝 htmlq
使用 apt / apt-get 命令在 Ubuntu 或 Debian Linux 上安裝 cargo 和 rustc:sudo apt install cargo
然後運行它。cargo install htmlq
macOS 安裝貨物
打開終端應用程序並運行端口命令,如下所示:sudo port install cargo
或者,您可以在 macOS 上安裝 Homebrew 並使用 brew 包管理器,如下所示:
brew install rustup # installs both cargo and rustc rustup-init rustc --version
FreeBSD安裝貨
使用 pkg 命令安裝 rustc,如下所示:sudo pkg install rust
了解如何安裝 Rust 其他操作系統..你現在有一個 rustc 工具和一個 cargo 工具。然後輸入以下簡單命令,在您的開發系統上獲取 htmlq。cargo install htmlq
設置路徑
一定要添加 $HOME/.cargo/bin 到 路徑變量 能夠使用導出命令運行已安裝的二進製文件
# sh/bash/ksh etc PATH="$PATH:$HOME/.cargo/bin" # tcsh/csh etc setenv PATH $PATH:$HOME/.cargo/bin
如何在 Linux 或 Unix 上使用 htmlq 從 HTML 文件中提取內容
讓我們使用 curl 命令按 ID 搜索頁面部分。curl -s url | htmlq '#css-selector'
curl -s url2 | htmlq '#css-selector'
curl -s https://www.cyberciti.biz/faq/ | htmlq --pretty '#content' | more
找到頁面上的所有鏈接。例如:
curl -s https://www.nixcraft.com | htmlq --attribute href a
得到幫助
無論如何運行:htmlq --help
htmlq 0.0.1 Michael Maclean <[email protected]> Runs CSS selectors on HTML USAGE: htmlq [FLAGS] [OPTIONS] <selector>... FLAGS: -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 -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
全部的
htmlq 無疑是一個不錯的工具,我真的很喜歡它。執行 查看 Github 源代碼..試一試,並在下面的評論部分告訴我們您喜歡什麼。