首页 > 解决方案 > 在 node.js 中显示为 html

问题描述

let request = require('request');

let cheerio = require('cheerio');

const $url = 'http://kradata.kra.co.kr:8082/service/api15/getOpenDataList';

const $KEY = 'xxxxxxxxxxxxxxxxxxxxxxxx';

const $meet = '1';

const $hr_no = '037703';

const $api_url = $url + '?meet=' + $meet + '&hr_no=' + $hr_no + '&ServiceKey=' + $KEY;

console.log($api_url);



request($api_url, function(err, res, body) {

    $ = cheerio.load(body);

    $('item').each(function(idx){

        let no1 = $(this).find('hrName').text();

        let no2 = $(this).find('winRateT').text();

        let no3 = $(this).find('qnlRateT').text();

        let no4 = $(this).find('winRateY').text();

        let no5 = $(this).find('recentRcDist').text();

        let no6 = $(this).find('chaksunT').text();

        let no7 = $(this).find('chaksunY').text();

        let no8 = $(this).find('chaksun_6').text();



        console.log(`경주마 이름 : ${no1} \n 통산 승률: ${no2}, 통산 복승률: ${no3}, 최근 1년 승률: ${no4} \n 최근경주거리: ${no5}, 통산착순상금: ${no6}, 최근1년착순상금: ${no7}, 최근6개월수득상금: ${no8}`);             

 });});

如何在 html 中显示 console.log 中的数据?

标签: javascripthtmlnode.js

解决方案


如果你想显示你正在console.log()阅读的文本,我相信是这样的:

console.log(`경주마 이름 : ${no1} \n 통산 승률: ${no2}, 통산 복승률: ${no3}, 최근 1년 승률: ${no4} \n 최근경주거리: ${no5}, 통산착순상금: ${no6}, 최근1년착순상금: ${no7}, 최근6개월수득상금: ${no8}`);

然后只需添加一个document.write()调用:

document.write(`경주마 이름 : ${no1} \n 통산 승률: ${no2}, 통산 복승률: ${no3}, 최근 1년 승률: ${no4} \n 최근경주거리: ${no5}, 통산착순상금: ${no6}, 최근1년착순상금: ${no7}, 최근6개월수득상금: ${no8}`);

这会将页面上的当前文本替换为您正在输入的项目console.log()


推荐阅读