extract links with node
const cheerio = require('cheerio');
module.exports = async function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
const htmlContent = `
<!DOCTYPE html>
<html>
<head>
<title>DevOps Job Offer</title>
</head>
<body>
<h1>DevOps Engineer Job Offer</h1>
<p>We are hiring for a DevOps Engineer position. Here are some details:</p>
<ul>
<li>Position: DevOps Engineer</li>
<li>Type: Full-time</li>
<li>Location: Remote</li>
</ul>
<p>We offer competitive compensation and benefits. If you are interested, please apply through one of the following links:</p>
<ul>
<li><a href="https://example.com/apply">Apply Now</a></li>
<li><a href="https://example.com/careers">Explore other opportunities</a></li>
</ul>
</body>
</html>
`;
function extractLinks(htmlContent) {
const $ = cheerio.load(htmlContent);
const links = [];
$('a').each((index, element) => {
const href = $(element).attr('href');
if (href) {
links.push(href);
}
return links;
}
const extractedLinks = extractLinks(htmlContent);
if (extractedLinks.length > 0) {
context.res = {
body: extractedLinks
} else {
context.res = {
status: 404,
body: "No links found."
}
Imported from rifaterdemsahin.com · 2024