Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/425.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Node.js-无法使用ChereIO存储预期数据_Javascript_Node.js_Asynchronous_Web Scraping_Cheerio - Fatal编程技术网

Javascript Node.js-无法使用ChereIO存储预期数据

Javascript Node.js-无法使用ChereIO存储预期数据,javascript,node.js,asynchronous,web-scraping,cheerio,Javascript,Node.js,Asynchronous,Web Scraping,Cheerio,我正试图刮一个网站,但我有一个大问题的输出 我希望恢复站点上的名称,它会找到数据,但当我尝试存储此值时,我只能存储值“null”。 我是一名编程初学者,我没有学习过承诺、回调和异步,但我认为这与问题密切相关 这是我使用.text()时的Node.js代码 HTML: 但当我试图将“Jean-Dominique SENARD”数据存储在一个变量中以对其进行操作时,我做不到,因为它返回“undefined”或“null” 你能帮帮我吗?谢谢。我建议使用基于承诺的方法,这将提供最可读的代码 您可以从检

我正试图刮一个网站,但我有一个大问题的输出

我希望恢复站点上的名称,它会找到数据,但当我尝试存储此值时,我只能存储值“null”。

我是一名编程初学者,我没有学习过承诺、回调和异步,但我认为这与问题密切相关

这是我使用
.text()时的Node.js代码
HTML:
但当我试图将“Jean-Dominique SENARD”数据存储在一个变量中以对其进行操作时,我做不到,因为它返回“undefined”或“null”


你能帮帮我吗?谢谢。

我建议使用基于承诺的方法,这将提供最可读的代码

您可以从检索函数返回承诺

我还建议使用语法,这将进一步提高可读性

得到结果后,可以在testGetRequiredData()中进一步操作它

您也可以尝试替换该行:

domaine = $('div#presentationlien.FichePresentation__link.mt-13 p.fs-12 a.Link').text();

这将只给出名称(但是可能没有那么健壮!)

例如:

const cheerio = require('cheerio');
const http = require('follow-redirects/http');
const https = require('follow-redirects/https');

function getRequiredData(url) {
    return new Promise((resolve, reject) => {
        https.get(url, response => {
            response.on('data', chunk => {
                const $ = cheerio.load(chunk, { xmlMode : false });
                domaine = $('div#presentationlien.FichePresentation__link.mt-13 p.fs-12 a.Link').text()
                resolve(domaine);
            });
        }).on('error', err => {
            reject(err);
        });
    });
}

async function testGetRequiredData(entreprise) {
    try { 
        const url = `https://www.example.com/search?q=${entreprise}`;
        let result = await getRequiredData(url);
        // Do whatever you wish with the result..
        console.log("Result:", result);
    } catch (error) {
        console.error(`testGetRequiredData: An error occurred:`, error);
    }
}

// Replace the parameter here..
testGetRequiredData("put entreprise here!");

有两个元素的类为
p.fs-12 a.Link
。因此,如果你想选择第二种方法,你应该使用另一种方法

另外,
.text()
用于从html元素检索文本值。如果您想要获取html元素本身,就不应该使用它

您需要的查询是:

$('div#presentationlien.FichePresentation__link.mt-13 p.fs-12 a.Link').eq(2).parent();

您可以使用htt和https安装的模块。感谢@Safiresh的评论!当然,我们不局限于任何一个模块。。以承诺为基础的方案最好。
219 établissements événementsJean-Dominique SENARD
domaine = $('div#presentationlien.FichePresentation__link.mt-13 p.fs-12 a.Link').text();
domaine = $('div#presentationlien.FichePresentation__link.mt-13 p.fs-12 a.Link').last().text();
const cheerio = require('cheerio');
const http = require('follow-redirects/http');
const https = require('follow-redirects/https');

function getRequiredData(url) {
    return new Promise((resolve, reject) => {
        https.get(url, response => {
            response.on('data', chunk => {
                const $ = cheerio.load(chunk, { xmlMode : false });
                domaine = $('div#presentationlien.FichePresentation__link.mt-13 p.fs-12 a.Link').text()
                resolve(domaine);
            });
        }).on('error', err => {
            reject(err);
        });
    });
}

async function testGetRequiredData(entreprise) {
    try { 
        const url = `https://www.example.com/search?q=${entreprise}`;
        let result = await getRequiredData(url);
        // Do whatever you wish with the result..
        console.log("Result:", result);
    } catch (error) {
        console.error(`testGetRequiredData: An error occurred:`, error);
    }
}

// Replace the parameter here..
testGetRequiredData("put entreprise here!");
$('div#presentationlien.FichePresentation__link.mt-13 p.fs-12 a.Link').eq(2).parent();