2

With jq one can query data in JSON files from the command line, what is useful for example when writing bash scripts, which just need one entry from a JSON file, like this

jq '.[0] | {message: .commit.message, name: .commit.committer.name}'

(Example taken from the tutorial)

I wonder if there are similar tools for HTML, which may for example accept an XPath or CSS selector as query and return the matching element(s).

4
  • github.com/ericchiang/pup Commented Oct 15, 2021 at 16:32
  • 2
    github.com/mgdm/htmlq Commented Oct 15, 2021 at 16:36
  • @glennjackman maybe this was just too easy. It is not packaged by my distribution (yet), but probably not too hard to compile. Do you like to make it an answer?
    – allo
    Commented Oct 16, 2021 at 10:33
  • No need. You could write your own answer letting us know what solution you chose, and your reasons for doing so. Commented Oct 16, 2021 at 12:53

2 Answers 2

2

I found this one https://github.com/mgdm/htmlq. There are some examples on the github page. e,i you can do something like this:

curl --silent https://www.rust-lang.org/ | htmlq '#get-help'
1

There is pup, written in Go: https://github.com/ericchiang/pup. Here is an example from the author:

$ curl -s https://news.ycombinator.com/ | pup 'table table tr:nth-last-of-type(n+2) td.title a json{}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.