| 1 | # pup |
| 2 | |
| 3 | pup is a command line tool for processing HTML. It reads from stdin, |
| 4 | prints to stdout, and allows the user to filter parts of the page using |
| 5 | [CSS selectors](https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Getting_started/Selectors). |
| 6 | |
| 7 | Inspired by [jq](http://stedolan.github.io/jq/), pup aims to be a |
| 8 | fast and flexible way of exploring HTML from the terminal. |
| 9 | |
| 10 | ## Install |
| 11 | |
| 12 | Direct downloads are available through the [releases page](https://github.com/EricChiang/pup/releases/latest). |
| 13 | |
| 14 | If you have Go installed on your computer just run `go get`. |
| 15 | |
| 16 | go get github.com/ericchiang/pup |
| 17 | |
| 18 | If you're on OS X, use [Homebrew](http://brew.sh/) to install (no Go required). |
| 19 | |
| 20 | brew install https://raw.githubusercontent.com/EricChiang/pup/master/pup.rb |
| 21 | |
| 22 | ## Quick start |
| 23 | |
| 24 | ```bash |
| 25 | $ curl -s https://news.ycombinator.com/ |
| 26 | ``` |
| 27 | |
| 28 | Ew, HTML. Let's run that through some pup selectors: |
| 29 | |
| 30 | ```bash |
| 31 | $ curl -s https://news.ycombinator.com/ | pup 'table table tr:nth-last-of-type(n+2) td.title a' |
| 32 | ``` |
| 33 | |
| 34 | Okay, how about only the links? |
| 35 | |
| 36 | ```bash |
| 37 | $ curl -s https://news.ycombinator.com/ | pup 'table table tr:nth-last-of-type(n+2) td.title a attr{href}' |
| 38 | ``` |
| 39 | |
| 40 | Even better, let's grab the titles too: |
| 41 | |
| 42 | ```bash |
| 43 | $ curl -s https://news.ycombinator.com/ | pup 'table table tr:nth-last-of-type(n+2) td.title a json{}' |
| 44 | ``` |
| 45 | |
| 46 | ## Basic Usage |
| 47 | |
| 48 | ```bash |
| 49 | $ cat index.html | pup [flags] '[selectors] [display function]' |
| 50 | ``` |
| 51 | |
| 52 | ## Examples |
| 53 | |
| 54 | Download a webpage with wget. |
| 55 | |
| 56 | ```bash |
| 57 | $ wget http://en.wikipedia.org/wiki/Robots_exclusion_standard -O robots.html |
| 58 | ``` |
| 59 | |
| 60 | #### Clean and indent |
| 61 | |
| 62 | By default pup will fill in missing tags and properly indent the page. |
| 63 | |
| 64 | ```bash |
| 65 | $ cat robots.html |
| 66 | # nasty looking HTML |
| 67 | $ cat robots.html | pup --color |
| 68 | # cleaned, indented, and colorful HTML |
| 69 | ``` |
| 70 | |
| 71 | #### Filter by tag |
| 72 | |
| 73 | ```bash |
| 74 | $ cat robots.html | pup 'title' |
| 75 | <title> |
| 76 | Robots exclusion standard - Wikipedia, the free encyclopedia |
| 77 | </title> |
| 78 | ``` |
| 79 | |
| 80 | #### Filter by id |
| 81 | |
| 82 | ```bash |
| 83 | $ cat robots.html | pup 'span#See_also' |
| 84 | <span class="mw-headline" id="See_also"> |
| 85 | See also |
| 86 | </span> |
| 87 | ``` |
| 88 | |
| 89 | #### Filter by attribute |
| 90 | |
| 91 | ```bash |
| 92 | $ cat robots.html | pup 'th[scope="row"]' |
| 93 | <th scope="row" class="navbox-group"> |
| 94 | Exclusion standards |
| 95 | </th> |
| 96 | <th scope="row" class="navbox-group"> |
| 97 | Related marketing topics |
| 98 | </th> |
| 99 | <th scope="row" class="navbox-group"> |
| 100 | Search marketing related topics |
| 101 | </th> |
| 102 | <th scope="row" class="navbox-group"> |
| 103 | Search engine spam |
| 104 | </th> |
| 105 | <th scope="row" class="navbox-group"> |
| 106 | Linking |
| 107 | </th> |
| 108 | <th scope="row" class="navbox-group"> |
| 109 | People |
| 110 | </th> |
| 111 | <th scope="row" class="navbox-group"> |
| 112 | Other |
| 113 | </th> |
| 114 | ``` |
| 115 | |
| 116 | #### Pseudo Classes |
| 117 | |
| 118 | CSS selectors have a group of specifiers called ["pseudo classes"]( |
| 119 | https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes) which are pretty |
| 120 | cool. pup implements a majority of the relevant ones them. |
| 121 | |
| 122 | Here are some examples. |
| 123 | |
| 124 | ```bash |
| 125 | $ cat robots.html | pup 'a[rel]:empty' |
| 126 | <a rel="license" href="//creativecommons.org/licenses/by-sa/3.0/" style="display:none;"> |
| 127 | </a> |
| 128 | ``` |
| 129 | |
| 130 | ```bash |
| 131 | $ cat robots.html | pup ':contains("History")' |
| 132 | <span class="toctext"> |
| 133 | History |
| 134 | </span> |
| 135 | <span class="mw-headline" id="History"> |
| 136 | History |
| 137 | </span> |
| 138 | ``` |
| 139 | |
| 140 | ```bash |
| 141 | $ cat robots.html | pup ':parent-of([action="edit"])' |
| 142 | <span class="wb-langlinks-edit wb-langlinks-link"> |
| 143 | <a action="edit" href="//www.wikidata.org/wiki/Q80776#sitelinks-wikipedia" text="Edit links" title="Edit interlanguage links" class="wbc-editpage"> |
| 144 | Edit links |
| 145 | </a> |
| 146 | </span> |
| 147 | ``` |
| 148 | |
| 149 | For a complete list, view the [implemented selectors](#implemented-selectors) |
| 150 | section. |
| 151 | |
| 152 | |
| 153 | #### `+`, `>`, and `,` |
| 154 | |
| 155 | These are intermediate characters that declare special instructions. For |
| 156 | instance, a comma `,` allows pup to specify multiple groups of selectors. |
| 157 | |
| 158 | ```bash |
| 159 | $ cat robots.html | pup 'title, h1 span[dir="auto"]' |
| 160 | <title> |
| 161 | Robots exclusion standard - Wikipedia, the free encyclopedia |
| 162 | </title> |
| 163 | <span dir="auto"> |
| 164 | Robots exclusion standard |
| 165 | </span> |
| 166 | ``` |
| 167 | |
| 168 | #### Chain selectors together |
| 169 | |
| 170 | When combining selectors, the HTML nodes selected by the previous selector will |
| 171 | be passed to the next ones. |
| 172 | |
| 173 | ```bash |
| 174 | $ cat robots.html | pup 'h1#firstHeading' |
| 175 | <h1 id="firstHeading" class="firstHeading" lang="en"> |
| 176 | <span dir="auto"> |
| 177 | Robots exclusion standard |
| 178 | </span> |
| 179 | </h1> |
| 180 | ``` |
| 181 | |
| 182 | ```bash |
| 183 | $ cat robots.html | pup 'h1#firstHeading span' |
| 184 | <span dir="auto"> |
| 185 | Robots exclusion standard |
| 186 | </span> |
| 187 | ``` |
| 188 | |
| 189 | ## Implemented Selectors |
| 190 | |
| 191 | For further examples of these selectors head over to [MDN]( |
| 192 | https://developer.mozilla.org/en-US/docs/Web/CSS/Reference). |
| 193 | |
| 194 | ```bash |
| 195 | pup '.class' |
| 196 | pup '#id' |
| 197 | pup 'element' |
| 198 | pup 'selector + selector' |
| 199 | pup 'selector > selector' |
| 200 | pup '[attribute]' |
| 201 | pup '[attribute="value"]' |
| 202 | pup '[attribute*="value"]' |
| 203 | pup '[attribute~="value"]' |
| 204 | pup '[attribute^="value"]' |
| 205 | pup '[attribute$="value"]' |
| 206 | pup ':empty' |
| 207 | pup ':first-child' |
| 208 | pup ':first-of-type' |
| 209 | pup ':last-child' |
| 210 | pup ':last-of-type' |
| 211 | pup ':only-child' |
| 212 | pup ':only-of-type' |
| 213 | pup ':contains("text")' |
| 214 | pup ':nth-child(n)' |
| 215 | pup ':nth-of-type(n)' |
| 216 | pup ':nth-last-child(n)' |
| 217 | pup ':nth-last-of-type(n)' |
| 218 | pup ':not(selector)' |
| 219 | pup ':parent-of(selector)' |
| 220 | ``` |
| 221 | |
| 222 | You can mix and match selectors as you wish. |
| 223 | |
| 224 | ```bash |
| 225 | cat index.html | pup 'element#id[attribute="value"]:first-of-type' |
| 226 | ``` |
| 227 | |
| 228 | ## Display Functions |
| 229 | |
| 230 | Non-HTML selectors which effect the output type are implemented as functions |
| 231 | which can be provided as a final argument. |
| 232 | |
| 233 | #### `text{}` |
| 234 | |
| 235 | Print all text from selected nodes and children in depth first order. |
| 236 | |
| 237 | ```bash |
| 238 | $ cat robots.html | pup '.mw-headline text{}' |
| 239 | History |
| 240 | About the standard |
| 241 | Disadvantages |
| 242 | Alternatives |
| 243 | Examples |
| 244 | Nonstandard extensions |
| 245 | Crawl-delay directive |
| 246 | Allow directive |
| 247 | Sitemap |
| 248 | Host |
| 249 | Universal "*" match |
| 250 | Meta tags and headers |
| 251 | See also |
| 252 | References |
| 253 | External links |
| 254 | ``` |
| 255 | |
| 256 | #### `attr{attrkey}` |
| 257 | |
| 258 | Print the values of all attributes with a given key from all selected nodes. |
| 259 | |
| 260 | ```bash |
| 261 | $ cat robots.html | pup '.catlinks div attr{id}' |
| 262 | mw-normal-catlinks |
| 263 | mw-hidden-catlinks |
| 264 | ``` |
| 265 | |
| 266 | #### `json{}` |
| 267 | |
| 268 | Print HTML as JSON. |
| 269 | |
| 270 | ```bash |
| 271 | $ cat robots.html | pup 'div#p-namespaces a' |
| 272 | <a href="/wiki/Robots_exclusion_standard" title="View the content page [c]" accesskey="c"> |
| 273 | Article |
| 274 | </a> |
| 275 | <a href="/wiki/Talk:Robots_exclusion_standard" title="Discussion about the content page [t]" accesskey="t"> |
| 276 | Talk |
| 277 | </a> |
| 278 | ``` |
| 279 | |
| 280 | ```bash |
| 281 | $ cat robots.html | pup 'div#p-namespaces a json{}' |
| 282 | [ |
| 283 | { |
| 284 | "accesskey": "c", |
| 285 | "href": "/wiki/Robots_exclusion_standard", |
| 286 | "tag": "a", |
| 287 | "text": "Article", |
| 288 | "title": "View the content page [c]" |
| 289 | }, |
| 290 | { |
| 291 | "accesskey": "t", |
| 292 | "href": "/wiki/Talk:Robots_exclusion_standard", |
| 293 | "tag": "a", |
| 294 | "text": "Talk", |
| 295 | "title": "Discussion about the content page [t]" |
| 296 | } |
| 297 | ] |
| 298 | ``` |
| 299 | |
| 300 | Use the `-i` / `--indent` flag to control the intent level. |
| 301 | |
| 302 | ```bash |
| 303 | $ cat robots.html | pup -i 4 'div#p-namespaces a json{}' |
| 304 | [ |
| 305 | { |
| 306 | "accesskey": "c", |
| 307 | "href": "/wiki/Robots_exclusion_standard", |
| 308 | "tag": "a", |
| 309 | "text": "Article", |
| 310 | "title": "View the content page [c]" |
| 311 | }, |
| 312 | { |
| 313 | "accesskey": "t", |
| 314 | "href": "/wiki/Talk:Robots_exclusion_standard", |
| 315 | "tag": "a", |
| 316 | "text": "Talk", |
| 317 | "title": "Discussion about the content page [t]" |
| 318 | } |
| 319 | ] |
| 320 | ``` |
| 321 | |
| 322 | If the selectors only return one element the results will be printed as a JSON |
| 323 | object, not a list. |
| 324 | |
| 325 | ```bash |
| 326 | $ cat robots.html | pup --indent 4 'title json{}' |
| 327 | { |
| 328 | "tag": "title", |
| 329 | "text": "Robots exclusion standard - Wikipedia, the free encyclopedia" |
| 330 | } |
| 331 | ``` |
| 332 | |
| 333 | Because there is no universal standard for converting HTML/XML to JSON, a |
| 334 | method has been chosen which hopefully fits. The goal is simply to get the |
| 335 | output of pup into a more consumable format. |
| 336 | |
| 337 | ## Flags |
| 338 | |
| 339 | Run `pup --help` for a list of further options |