只是更新了我的代码,由于某种原因console.log()
并没有输出任何信息,为什么?
它现在返回此:
undefined
done
200
null
脚本:
const request = require('request');
const cheerio = require('cheerio');
request('https://kith.com/collections/footwear/products/jbaq4160-140', (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
const output = $(':contains("6")').attr('value');
console.log(output);
console.log('done');
}
});
答案 0 :(得分:0)
似乎您假设(!error && response.statusCode == 200)
。在支架后再放两个console.log。
request('https://kith.com/collections/footwear/products/jbaq4160-140', (error, response, html) => {
if (!error && response.statusCode == 200) {
const $ = cheerio.load(html);
const output = $(':contains("4.5")').attr('value');
console.log(output);
console.log('done');
}
console.log(response.statusCode);
console.log(error);
});
答案 1 :(得分:0)
添加这样的用户代理标题:
var options = {
headers: {'user-agent': 'request'}
}
request('https://kith.com/collections/footwear/products/jbaq4160-140', options, function(error, response, html) => {
答案 2 :(得分:0)
在您的代码中,替换:
const output = $(':contains("4.5")').attr('value');
通过
const output = $
我怀疑您的表达方式有问题:$(':contains("4.5")').attr('value')