Extract Links from an HTML File in Node.JS

Do you need to extract data from an HTML file? If so, we have an API that can easily assist in extracting resolved URLs (fully qualified if possible) from an input HTML file. To ensure the process runs smoothly you will need either the HTML file or the URL of a file to perform the operation on; you have the option to include the base URL of the page as well.

To get things started in Node.JS, we will run this command to install the SDK:

Or, add this snippet to your package.json:

Our next step is to create an instance of the API and call the function with the following code:

// Configure API key authorization: Apikey
var Apikey = defaultClient.authentications['Apikey'];
Apikey.apiKey = 'YOUR API KEY';
var apiInstance = new CloudmersiveConvertApiClient.EditHtmlApi();var opts = {
'inputFile': Buffer.from(fs.readFileSync("C:\\temp\\inputfile").buffer), // File | Optional: Input file to perform the operation on.
'inputFileUrl': "inputFileUrl_example", // String | Optional: URL of a file to operate on as input.
'baseUrl': "baseUrl_example" // String | Optional: Base URL of the page, such as https://mydomain.com
};
var callback = function(error, data, response) {
if (error) {
console.error(error);
} else {
console.log('API called successfully. Returned data: ' + data);
}
};
apiInstance.editHtmlHtmlGetLinks(opts, callback);

In no time at all, the returned information will display a list of the located links and their corresponding names/URLs. To retrieve your personal API key, head to the Cloudmersive website to register for a free account that will give you access to 800 monthly calls across our library of APIs.

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store