I have a question that I use Axios and Cheerio to get other web data about and take it into an array. Here I have an array
linkURLG = []
When console.log(linkURLG) it still releases the results but when I use it with
console.log(linkURLG.length) then it returns to 0 even though the array has 21 length
Where do I ask the problem and how to solve it? Thanks for reading.
const axios = require('axios')
const cheerio = require('cheerio')
const fs = require('fs')
let linkURLG = []
async function getData() {
try {
const url = 'https://viblo.asia/newest'
const { data } = await axios({
method: 'GET',
url: url,
// headers: {
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36'
// }
})
const $ = cheerio.load(data)
const elementUrl = $('.post-feed-item')
const dataGet = []
const linkUrls = []
$(elementUrl).each((index, element) => {
const author = $(element).find('span.el-popover__reference-wrapper').text().trim()
const authorAvt = $(element).find('a.d-flex.mr-05 > img').attr('src')
const title = $(element).find('h3.word-break.mr-05').text()
const url = 'https://viblo.asia' + $(element).find('h3.word-break.mr-05 > a').attr('href')
const view = $(element).find('div.stats > span').first().text().trim()
dataGet[index] = {author, authorAvt, title, url, view}
linkUrls[index] = {url}
linkURLG[index] = {url}
})
// console.log(dataGet)
// console.log(linkUrls)
document.getElementById('renderer-contain').innerHTML = dataGet.map(dataGet => `
<div class="card">
<div class="card-avt">
<div class="card-avt-img"><img src="${dataGet.authorAvt}"></div>
</div>
<div class="card-contain">
<div class="author"><span>Author</span> ${dataGet.author}</div>
<div class="card-contain-title"><a href="${dataGet.url}">${dataGet.title}</a></div>
<div class="view"><span>Lượt xem:</span> ${dataGet.view}</div>
</div>
</div>
`).join('')
} catch (error) {
console.log(error)
}
}
getData()
console.log(linkURLG)
async function getContent() {
try {
const url = 'https://viblo.asia/newest'
const { data } = await axios({
method: 'GET',
url: url,
// headers: {
// 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.88 Safari/537.36'
// }
})
const $ = cheerio.load(data)
} catch (error) {
console.log(error)
}
}
/* styles.css */
@import url('https://fonts.googleapis.com/css2?family=Roboto&display=swap');
/* Add styles here to customize the appearance of your app */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
background-color: rgb(19, 19, 19);
margin: auto;
font-family: 'Roboto', sans-serif;
}
#renderer-contain {
display: flex;
gap: 5px;
flex-direction: column;
margin: 10px 10px;
}
.card {
margin: auto;
width: 90%;
/* color: white; */
background-color: white;
border-radius: 5px;
padding: 10px;
display: flex;
flex-direction: row;
gap: 10px;
}
.card-avt {
display: flex;
justify-content: center;
align-items: center;
}
.card-avt-img > img {
width: 60px;
border-radius: 5px;
}
.card-contain {
display: flex;
gap: 5px;
flex-direction: column;
}
.author > span {
background-color: rgba(0, 255, 255, 0.329);
padding: 0px 5px 0px 5px;
border: .5px solid rgba(0, 255, 255, 0.329);
color: rgba(2, 91, 91, 0.566);
border-radius: 5px;
}
.author {
display: flex;
flex-direction: row;
align-items: center;
gap: 2px;
}
.card-contain .view {
font-size: 12px;
}
.card-contain-title > a:link {
text-decoration: none;
font-weight: bold;
color: rgb(71, 71, 255);
}
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<!-- <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'"> -->
<link href="./styles.css" rel="stylesheet">
<title>getApi</title>
</head>
<body>
<div id="renderer-contain"></div>
<!-- You can also require other files to run in this process -->
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="./renderer.js"></script>
</body>
</html>
