Linuxコマンドラインツールを使用してWebページからhtmlタグとその属性を取得しようとしています。以下は具体的なケースです。
タスクは次のとおりです。ウェブサイト「clojurescript.net」のすべての「script」タグのすべての「src」属性を取得します。これは可能な限り最小限の意識で行う必要があり、特定のテキスト行を取得するためにgrepを使用するのと同じくらい簡単です。
curl -L clojurescript.net | [the toolchain in question "script @src"]
http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
http://kanaka.github.io/cljs-bootstrap/web/jqconsole.min.js
http://kanaka.github.io/cljs-bootstrap/web/jq_readline.js
[...further results]
私が試したツールはhxnormalize / hxselect、tidy、xmlstarletです。誰も信頼できる結果を得ることはできません。複数のプログラミング言語用のライブラリを使用する場合、これは常に簡単です。
- それでは、CLIでこれを行うための最新の技術は何ですか?
- ツリーをより明確に表現するには、まずHTMLをXMLに変換するのが合理的ではありませんか?
- 通常、HTMLは多くの構文エラーで書かれています。この緩い構造を修正/整理するための基本的な方法(パブリックライブラリで使用されています)はありますか?
属性のみを抽出する追加のオプションと一緒にCSSセレクタを使用できます。しかし、おそらくXPATHはより良い構文選択かもしれません。
ベストアンサー1
そして
curl "http://clojurescript.net/" | scrape -be '//body/script' | xml2json | jq '.html.body.script[].src
あなたは
"http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"
"http://kanaka.github.io/cljs-bootstrap/web/jqconsole.min.js"
"http://kanaka.github.io/cljs-bootstrap/web/jq_readline.js"
"http://kanaka.github.io/cljs-bootstrap/web/repl-web.js"
"http://kanaka.github.io/cljs-bootstrap/web/repl-main.js"
これらのツールは次のとおりです。
- すごいJQhttps://stedolan.github.io/jq/;
- 刈るhttps://github.com/jeroenjanssens/data-science-at-the-command-line/blob/master/tools/scrape;
- xml2jsonhttps://github.com/Inist-CNRS/node-xml2json-command。
または以下を使用して:
curl "http://clojurescript.net/" | hxnormalize -x | hxselect -i 'body > script' | grep -oP '(http:.*?)(")' | sed 's/"//g'
あなたは:
http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
http://kanaka.github.io/cljs-bootstrap/web/jqconsole.min.js
http://kanaka.github.io/cljs-bootstrap/web/jq_readline.js
http://kanaka.github.io/cljs-bootstrap/web/repl-web.js
http://kanaka.github.io/cljs-bootstrap/web/repl-main.js