類似 jq( json 處理器),但是是針對 HTML 檔案格式

jq:JSON 資料的 sed–你可以用它來切分、過濾、對映和轉換結構化資料,htmlq 功能類似 jq,是用來處理 html 內容,可使用 CSS 選擇器從 HTML 檔案中提取一些內容。所以可以使用這個命令列工具搭配 shell script 來做一個簡單的網頁爬蟲

安裝

可使用 cargo 或是 homebrew 安裝

brew install htmlq

使用範例

與 cURL 一起使用,透過 ID 找到頁面的一部分

$ curl --silent https://www.rust-lang.org/ | htmlq '#get-help'
<div class="four columns mt3 mt0-l" id="get-help">
        <h4>Get help!</h4>
        <ul>
          <li><a href="https://doc.rust-lang.org">Documentation</a></li>
          <li><a href="https://users.rust-lang.org">Ask a Question on the Users Forum</a></li>
          <li><a href="http://ping.rust-lang.org">Check Website Status</a></li>
        </ul>
        <div class="languages">
            <label class="hidden" for="language-footer">Language</label>
            <select id="language-footer">
                <option title="English (US)" value="en-US">English (en-US)</option>
<option title="French" value="fr">Français (fr)</option>
<option title="German" value="de">Deutsch (de)</option>

            </select>
        </div>
      </div>

查詢一個頁面中的所有連結

$ curl --silent https://www.rust-lang.org/ | htmlq --attribute href a
/
/tools/install
/learn
/tools
/governance
/community
https://blog.rust-lang.org/
/learn/get-started
https://blog.rust-lang.org/2019/04/25/Rust-1.34.1.html
https://blog.rust-lang.org/2018/12/06/Rust-1.31-and-rust-2018.html
[...]

獲取一個網頁中的文字內容

$ curl --silent https://nixos.org/nixos/about.html | htmlq  --text .main

          About NixOS

NixOS is a GNU/Linux distribution that aims to
improve the state of the art in system configuration management.  In
existing distributions, actions such as upgrades are dangerous:
upgrading a package can cause other packages to break, upgrading an
entire system is much less reliable than reinstalling from scratch,
you can’t safely test what the results of a configuration change will
be, you cannot easily undo changes to the system, and so on.  We want
to change that.  NixOS has many innovative features:

[...]

bat 高亮顯示語法

$ curl --silent example.com | htmlq 'body' | bat --language html

專案網址

也許你會有興趣

不受 FB 演算法影響,歡迎透過 e-mail 訂閱網站更新

不受 FB 演算法影響,歡迎透過 e-mail 訂閱網站更新

支持 Soft & Share

❤️您應該有留意到,我們的網頁並不會出現干擾人的跳出煩人的廣告或是在內容中嵌入廣告,因為我們發現這樣對閱讀網頁的內容體驗真的是不好!

如果您覺得我們提供的內容服務還不錯,歡迎透過對以下產品/服務的購買投資來支持本站的營運走得更遠

如果暫時還不需要以下的付費服務,幫我們把這個網站分享給有需要的朋友,您的小小舉動會對 Soft & Share 有莫大的幫助!感謝您的支持!

🎈如果您點選優惠連結後,還是沒有看到優惠價格,請將瀏覽器的 cookie 清除 ( 清除 udemy 網站的就可以了 ),然後重新點選優惠連結並登入 Udemy 就可以了

找相關課程?試看看 Soft & Share 網站搜尋引擎

✍ 搜尋結果太多?可參考 Soft & Share 搜尋引擎使用技巧

Comments are closed.

由 WordPress.com 建置.

Up ↑

探索更多來自 Soft & Share 的內容

立即訂閱即可持續閱讀,還能取得所有封存文章。

Continue reading