-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.js
52 lines (43 loc) · 1.48 KB
/
hello.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const express = require('express')
const {default : axios} = require('axios')
const PORT = process.env.PORT || 8000
const cheerio = require('cheerio')
const { response } = require('express')
const app = express()
const articles = []
const climateLink = []
app.get('/', (req, res) => {
res.json('This is My API')
})
app.get('/news', (req, res) => {
axios.get(`https://timesofindia.indiatimes.com/topic/climate-change`)
.then((response) => {
const html = response.data
const $ = cheerio.load(html)
const textContains = $('a:contains("Climate")', html).each(function () {
const title = $(this).text()
const url = $(this).attr('href')
articles.push({
title,
url
})
})
}).catch((err) => console.log(err))
for (let index = 2; index < 15; index++) {
axios.get(`https://timesofindia.indiatimes.com/topic/climate-change/${index}`)
.then((response) => {
const html = response.data
const $ = cheerio.load(html)
const textContains = $('a:contains("Climate")', html).each(function () {
const title = $(this).text()
const url = $(this).attr('href')
articles.push({
title,
url
})
})
}).catch((err) => console.log(err))
}
res.json(articles)
})
app.listen(PORT, () => console.log("Listening Port 8000"))