mgdm / htmlq

Like jq, but for HTML.
MIT License
7k stars 107 forks source link

JSON output #22

Open tercerapersona opened 2 years ago

tercerapersona commented 2 years ago

Have you thought about adding JSON output?

-t, --text                 Output only the contents of text nodes inside selected elements
-j, --json                 Output JSON representation of the selected elements

It would be great to curl + parse with htmlq and finally pipe to jq ;)

mazznoer commented 2 years ago

You can do that right now with pup.

aborruso commented 2 years ago

A way is to use xq.

Running

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

you have

{
  "div": {
    "@class": "flex flex-column mw8 w-100 measure-wide-l pv2 pv5-m pv2-ns ph4-m ph4-l",
    "@id": "get-help",
    "h4": "Get help!",
    "ul": {
      "li": [
        {
          "a": {
            "@href": "/learn",
            "#text": "Documentation"
          }
        },
        {
          "a": {
            "@href": "http://forge.rust-lang.org",
            "#text": "Rust Forge (Contributor Documentation)"
          }
        },
        {
          "a": {
            "@href": "https://users.rust-lang.org",
            "#text": "Ask a Question on the Users Forum"
          }
        },
        {
          "a": {
            "@href": "http://ping.rust-lang.org",
            "#text": "Check Website Status"
          }
        }
      ]
    },
    "div": {
      "@class": "languages",
      "label": {
        "@class": "hidden",
        "@for": "language-footer",
        "#text": "Language"
      },
      "select": {
        "@id": "language-footer",
        "option": [
          {
            "@title": "English (en-US)",
            "@value": "en-US",
            "#text": "English (en-US)"
          },
          {
            "@title": "Español (es)",
            "@value": "es",
            "#text": "Español (es)"
          },
          {
            "@title": "Français (fr)",
            "@value": "fr",
            "#text": "Français (fr)"
          },
          {
            "@title": "Italiano (it)",
            "@value": "it",
            "#text": "Italiano (it)"
          },
          {
            "@title": "日本語 (ja)",
            "@value": "ja",
            "#text": "日本語 (ja)"
          },
          {
            "@title": "Português (pt-BR)",
            "@value": "pt-BR",
            "#text": "Português (pt-BR)"
          },
          {
            "@title": "Русский (ru)",
            "@value": "ru",
            "#text": "Русский (ru)"
          },
          {
            "@title": "Türkçe (tr)",
            "@value": "tr",
            "#text": "Türkçe (tr)"
          },
          {
            "@title": "简体中文 (zh-CN)",
            "@value": "zh-CN",
            "#text": "简体中文 (zh-CN)"
          },
          {
            "@title": "正體中文 (zh-TW)",
            "@value": "zh-TW",
            "#text": "正體中文 (zh-TW)"
          }
        ]
      }
    }
  }
}
SuperSandro2000 commented 2 years ago

Is there any progress here?