get-hrefs
Get all href urls from an HTML string
Last updated a year ago by joakimbeng .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install get-hrefs 
SYNC missed versions from official npm registry.

get-hrefs

Build status NPM version XO code style

Get all href urls from an HTML string

Installation

Install get-hrefs using npm:

npm install --save get-hrefs

Usage

Module usage

const getHrefs = require('get-hrefs');

getHrefs(`
	<body>
		<a href="http://example.com">Example</a>
	</body>
`);
// ["http://example.com"]

getHrefs(`
	<head>
		<base href="http://example.com/path1/">
	</head>
	<body>
		<a href="path2/index.html">Example</a>
	</body>
`);
// ["http://example.com/path1/path2/index.html"]

CLI usage

$> get-hrefs --help

Get all href urls from an HTML string

  Usage:
    get-hrefs <html file>
    cat <html file> | get-hrefs

  Options:
    -b, --base-url	Set baseUrl
    <all other flags are passed to normalize-url>

  Examples:
    curl -s example.com | get-hrefs
    echo '<a href="http://www.example.com">Link</a>' | get-hrefs --strip-w-w-w

API

getHrefs(html, [options])

Name Type Description
html String The HTML string to extract hrefs from
options Object Optional options

Returns: Array<String>, all unique and normalized hrefs resolved from any provided baseUrl and <base href="..."> in the HTML document.

options.baseUrl

Type: String
Default: ""

The baseUrl to use for relative hrefs. The module also takes <base ...> tags into account.

options.allowedProtocols

Type: Object
Default: {"http": true, "https": true}

Specifies which protocols to allow by setting their respective key (the protocol name without ":") in allowedProtocols to true (or to false to disable one of the defaults), e.g. allowedProtocols: {tel: true, http: false} will return only found URLs with the protocols tel: or https:.

options.<any>

All other options are passed to normalize-url. See its options for alternatives.

Related modules

License

MIT © Joakim Carlstein

Current Tags

  • 4.0.0                                ...           latest (a year ago)

7 Versions

  • 4.0.0                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.0                                ...           3 years ago
  • 1.0.3                                ...           4 years ago
  • 1.0.2                                ...           4 years ago
  • 1.0.1                                ...           4 years ago
  • 1.0.0                                ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (4)
Dev Dependencies (8)

Copyright 2014 - 2016 © taobao.org |