A collection of articles about cult filmmaker and King of Filth, John Waters
Using Node.js and Express, this basic API uses the npm package cheerio to scrape websites for <a>
tags that contain "John Waters":
- The Advocate
- Baltimore Magazine
- Dazed
- The Guardian
- IndieWire
- The New York Times
- Out...and many more!
- Clone this project
cd
into the project from your terminalnpm i
to install dependenciesnpm start
to start local server- Open browser to
localhost:8000
//Here, we run through our articles array and forEach site, we establish a cheerio variable and scrape
// the site for a couple of variations of "John Waters" contained within the anchor tags
articles.forEach(site => {
axios.get(site.address)
.then((response) => {
const html = response.data
const $ = cheerio.load(html)
// looking for anchor tags that contain "John Waters" within emphasis tags
$('a:contains("<em>John Waters</em>")', html).each(function() {
const title = $(this).text().replace(/[^0-9a-z-A-Z ]/g, "").replace(/ +/, " ")
const url = $(this).attr('href')
// if we find tags that are not advertisements
if (title !== "" && title !== "_" && title !== 'acontains' && !title.includes('discount') && !title.includes('Accessories') && !title.includes('mediamax-width') && url) {
// push them into a madia array with defined key/values
media.push({
title,
url: site.base ? site.base + url : url,
source: site.name
})
}
})
// looking for anchor tags that contain "John Waters"
$('a:contains("John Waters")', html).each(function() {
const title = $(this).text().replace(/[^0-9a-z-A-Z ]/g, "").replace(/ +/, " ")
const url = $(this).attr('href')
// if we find tags that are not advertisements
if (title !== "" && title !== "_" && title !== 'acontains' && !title.includes('discount') && url && title.length < 300 && url.length < 300) {
// push them into a madia array with defined key/values
media.push({
title,
url: site.base ? site.base + url : url,
source: site.name
})
}
})
})
})
- GET all articles
var axios = require("axios").default;
var options = {
method: 'GET',
url: 'https://filthyapi.p.rapidapi.com/articles',
headers: {
'x-rapidapi-host': 'filthyapi.p.rapidapi.com',
'x-rapidapi-key': '07c49e7131msh1df335cef06dbe8p1c0393jsn50aa8ba72cbf'
}
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
- GET article by specific source
var axios = require("axios").default;
var options = {
method: 'GET',
url: 'https://filthyapi.p.rapidapi.com/articles/theadvocate',
headers: {
'x-rapidapi-host': 'filthyapi.p.rapidapi.com',
'x-rapidapi-key': '07c49e7131msh1df335cef06dbe8p1c0393jsn50aa8ba72cbf'
}
};
axios.request(options).then(function (response) {
console.log(response.data);
}).catch(function (error) {
console.error(error);
});
Node.js | Express | npm | Axios | Heroku |
Built with the help of developer GODDESS Ania Kubow
- YouTube walkthrough (https://youtu.be/GK4Pl-GmPHk)
Beth Meeker GH |