Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/412.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 节点js中innertext的结果_Javascript_Node.js_Puppeteer - Fatal编程技术网

Javascript 节点js中innertext的结果

Javascript 节点js中innertext的结果,javascript,node.js,puppeteer,Javascript,Node.js,Puppeteer,我现在正在跟随教程,以了解更多关于使用木偶演员刮网站。他/她为此目的使用网站。我们在完成教程后得到的代码是 const puppeteer = require('puppeteer'); let scrape = async () => { const browser = await puppeteer.launch({headless: false}); const page = await browser.newPage(); await page.goto('http://boo

我现在正在跟随教程,以了解更多关于使用木偶演员刮网站。他/她为此目的使用网站。我们在完成教程后得到的代码是

const puppeteer = require('puppeteer');

let scrape = async () => {
const browser = await puppeteer.launch({headless: false});
const page = await browser.newPage();

await page.goto('http://books.toscrape.com/');
await page.click('#default > div > div > div > div > section > div:nth-child(2) > ol > li:nth-child(1) > article > div.image_container > a > img');
await page.waitFor(1000);

const result = await page.evaluate(() => {
    let title = document.querySelector('h1').innerText;
    let price = document.querySelector('.price_color').innerText;

    return {
        title,
        price
    }

});

browser.close();
return result;
};

scrape().then((value) => {
console.log(value); // Success!
});
运行此代码后的输出是

 { title: 'A Light in the Attic', price: '£51.77' }
我理解所有这些,但我想再进一步。也就是说,我想提取价格51.77,并进一步使用该价格在同一脚本中对其进行计算。我尝试了以下方法,但失败了

scrape().then((value) => {
const str=value;
const fl=parseFloat(str.substring(42,46));
fl=2*fl;
console.log('result is',fl);
});

我想我不完全理解innerText函数是如何工作的,它到底输出了什么。

我认为你应该用这种方式解析价格值,它应该可以工作

scrape().then((value) => {
const str = value;
const fl = parseFloat(str.price);
fl=2*fl;
console.log('result is',fl);
});

我认为你应该用这种方式解析价格值,它应该是有效的

scrape().then((value) => {
const str = value;
const fl = parseFloat(str.price);
fl=2*fl;
console.log('result is',fl);
});
value是从scrape()返回的结果,因此value是

value:{ title: 'A Light in the Attic', price: '£51.77' }
要获取价格,必须使用“.” 您的代码应该如下所示:

scrape().then((value) => {
const str=value.price
let fl=parseFloat(str.slice(1));// slice to remove the first character
fl=2*fl;
console.log('result is',fl);
});
value是从scrape()返回的结果,因此value是

value:{ title: 'A Light in the Attic', price: '£51.77' }
要获取价格,必须使用“.” 您的代码应该如下所示:

scrape().then((value) => {
const str=value.price
let fl=parseFloat(str.slice(1));// slice to remove the first character
fl=2*fl;
console.log('result is',fl);
});

您的
不是字符串,而是具有标题和价格属性的对象。因此,您可以通过
value.price
访问价格

或者,您可以通过解构将参数写成
{title,price}
,而不是
value

此外,如果您希望稍后为其重新分配另一个值,则不能将
fl
声明为常量

从价格中删除货币符号和其他非数字符号的可靠方法是通过正则表达式匹配:

scrape().then(({title, price}) => {
  let fl = +price.match(/\d+.\d+/)[0];
  fl = 2 * fl;
  console.log('result is', fl);
});

根据您的需要,如果没有有效的价格,
price.match
返回
null
,您可能仍希望处理这种情况

您的
不是字符串,而是具有标题和价格属性的对象。因此,您可以通过
value.price
访问价格

或者,您可以通过解构将参数写成
{title,price}
,而不是
value

此外,如果您希望稍后为其重新分配另一个值,则不能将
fl
声明为常量

从价格中删除货币符号和其他非数字符号的可靠方法是通过正则表达式匹配:

scrape().then(({title, price}) => {
  let fl = +price.match(/\d+.\d+/)[0];
  fl = 2 * fl;
  console.log('result is', fl);
});
根据您的需要,如果没有有效的价格,
price.match
返回
null
,您可能仍希望处理这种情况