Skip to content
microlinkhqPublic

About

Get the HTML from any website, fine-tuned for correction & speed

Topics

Resources

Stars

102 stars

Watchers

3 watching

Forks

Repository files navigation

microlink logo microlink logo

Last version Coverage Status NPM Status

Get the HTML from any website, fine-tuned for correction & speed.

Features

  • Get HTML markup for any URL, including images, video, audio, pdf, or office documents (docx, xlsx, pptx, odt, rtf, epub).
  • Block ads tracker or any non-necessary network subrequest.
  • Handle unreachable or timeout URLs gracefully.
  • Ensure HTML markup is appropriately encoded.

html-get takes advantage of puppeteer headless technology when is needed, such as client-side apps that needs to be prerender.

Install

$ npm install browserless puppeteer html-get --save

Usage

const createBrowserless = require('browserless')
const getHTML = require('html-get')

// Spawn Chromium process once
const browserlessFactory = createBrowserless()

// Kill the process when Node.js exit
process.on('exit', () => {
  console.log('closing resources!')
  browserlessFactory.close()
})

const getContent = async url => {
  // create a browser context inside Chromium process
  const browserContext = browserlessFactory.createContext()
  const getBrowserless = () => browserContext
  const result = await getHTML(url, { getBrowserless })
  // close the browser context after it's used
  await getBrowserless((browser) => browser.destroyContext())
  return result
}

getContent('https://example.com')
  .then(content => {
    console.log(content)
    process.exit()
  })
  .catch(error => {
    console.error(error)
    process.exit(1)
  })

Command Line

$ npx html-get https://example.com

API

getHTML(url, [options])

url

Required
Type: string

The target URL for getting the HTML markup.

options

encoding

Type: string Default: 'utf-8'

It ensures the HTML markup is encoded to the encoded value provided.

The value will be passes to html-encode

getBrowserless

Required
Type: function

A function that should return a browserless instance to be used for interact with puppeteer:

getMode

Type: function

It determines the strategy to use based on the url, being the possibles values 'fetch' or 'prerender' .

getTemporalFile

Type: function

It receives the URL and the file extension, and returns { path } for a file html-get writes the fetched PDF or office document to, and the converted HTML of a large PDF. Every call must return a new path: two fetches of the same URL at once would otherwise read each other's file. The default is a random name in os.tmpdir().

html-get removes every file it asked for once the fetch ends, whether the conversion succeeded, failed or was canceled.

gotOpts

Type: object

It passes configuration object to got under 'fetch' strategy.

headers

Type: object

Request headers that will be passed to fetch/prerender process.

mutool

Type: function|boolean
Default: source code

It returns a function that receives that executes mutool binary for turning PDF files into HTML markup.

It can explicitly disabled passing false.

A custom function receives the arguments and { signal, timeout }: timeout is what is left of the fetch timeout, and signal aborts when the fetch is canceled. The default runner kills mutool with SIGKILL on either.

The PDF is converted with -O preserve-images=no, because mutool otherwise inlines every image as base64: a 151MB scanned PDF of 688 pages became 232MB of HTML, and parsing it took a 7GB heap. MuPDF 1.19 accepts the option and ignores it; 1.28 honors it (the same PDF is 35MB of HTML).

The PDF reader only looks at page one and skips images of more than 400x400 pixels in total, with a side under 16px, or used as stencil masks. When it finds none for og:image, mutool draws the PDF one page at a time, up to the first 20, and the first image it inlines in a page becomes og:image: the image a metadata reader picked from the inlined HTML before. A custom function receives <file> <page> for those calls and must resolve { stdout } with mutool's HTML, whose <div id="pageN"> tells the search which page it got; output without it ends the search.

pandoc

Type: function|boolean
Default: source code

It returns a function that executes the pandoc binary for turning office documents (docx, xlsx, pptx, odt, rtf, epub) into HTML markup.

The format is resolved from the response content-type, falling back to the URL extension.

It can be explicitly disabled passing false.

A custom function receives (format, filepath, { signal, timeout }), with the same meaning as for mutool. The default runner kills pandoc with SIGKILL on either, so a conversion never outlives its fetch: pandoc has no limit of its own, and a 4.5MB spreadsheet once took it 34s and 10.9GB.

prerender

Type: boolean|string
Default: 'auto'

Enable or disable prerendering as mechanism for getting the HTML markup explicitly.

The value auto means that that internally use a list of websites that don't need to use prerendering by default. This list is used for speedup the process, using fetch mode for these websites.

See getMode parameter for know more.

puppeteerOpts

Type: object

It passes coniguration object to puppeteer under 'prerender' strategy.

rewriteUrls

Type: boolean
Default: false

When is true, it will be rewritten CSS/HTML relatives URLs present in the HTML markup into absolutes.

rewriteHtml

Type: boolean
Default: false

When is true, it will rewrite some common mistake related with HTML meta tags.

serializeHtml

It determines how HTML should be serialied before returning.

It's serialized $ => ({ html: $.html() }) by default.

License

html-get © Microlink, released under the MIT License.
Authored and maintained by Kiko Beats with help from contributors.

microlink.io · GitHub microlinkhq · X @microlinkhq

About

Get the HTML from any website, fine-tuned for correction & speed

Topics

Resources

Stars

102 stars

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages