我的硬盘上有一些html文件,我想用jquery来从中提取数据.这可以用cheerio吗?我试过给当地的路径提供cheerio,但它不起作用.我有一个想法是在节点中创建一个Web服务器,从html文件中读取,然后通过服务器将其传送给cheerio-这样吗
我正在使用cheerio lib并尝试获取此脚本字段 - script type="application/json"
但由于某种原因,它无法找到这些脚本标记.怎么了?我该如何解决?
var $ = require('cheerio')
var parsedHTML = $.load(html)
console.log( parsedHTML('script').get().length ); // this is 0
Run Code Online (Sandbox Code Playgroud) 我是前python开发人员,我已经使用BS4几年了现在我正在开发节点和是cheerio包是非常好的,但我需要像BS4一样的smth在节点中抓取
是否有一些替代cheerio?谢谢!
我像Cheerio一样解析我的请求:
var url = http://shop.nag.ru/catalog/16939.IP-videonablyudenie-OMNY/16944.IP-kamery-OMNY-c-vario-obektivom/16704.OMNY-1000-PRO;
request.get(url, function (err, response, body) {
console.log(body);
$ = cheerio.load(body);
console.log($(".description").html());
});
Run Code Online (Sandbox Code Playgroud)
作为输出,我看到内容,但在不可读的奇怪编码:
//Plain body console.log(body) (p.s. russian chars):
<h1><span style="font-size: 16px;">??????? 3?? IP HD ?????? OMNY - ?????????? ????? ?????</span></h1><p style
// cheerio's console.log $(".description").html()
<h1><span style="font-size: 16px;">Уличная 3Мп IP HD камера OMNY
Run Code Online (Sandbox Code Playgroud)
目标URL链接编码采用UTF-8格式.那么为什么Cheerio会破坏我的编码呢?
试图使用iconv来编码我的身体反应:
var body1 = iconv.decode(body, "utf-8");
Run Code Online (Sandbox Code Playgroud)
但console.log($(".description").html());仍然会返回奇怪的文字.
我可以用meteor.js刮掉吗?刚发现cheerio哪种作品非常出色request.我可以将这些用于流星,还是有类似的东西?
你有一个有效的例子吗?
我有一些像这样的HTML:
<span id="cod">Code:</span> <span>12345</span>
<span>Category:</span> <span>faucets</span>
Run Code Online (Sandbox Code Playgroud)
我想获取类别名称("水龙头").这是我的试用版:
var $ = cheerio.load(html.contents);
var category = $('span[innerHTML="Category:"]').next().text();
Run Code Online (Sandbox Code Playgroud)
但这不起作用(innerHTML修饰符不选择任何东西).
任何线索?
最近我试图使用Nodejs,请求模块和cheerio 从网站(kicktipp)抓取信息.由于此站点需要身份验证才能查看其大部分站点,因此我尝试通过发布请求登录并检查用户是否使用以下代码登录(我使用虚拟数据替换了凭据但我在实际脚本中使用了真实数据):
var request = require('request');
var jar = request.jar();
var request = request.defaults({
jar: jar,
followAllRedirects: true
});
var jar = request.jar();
var cheerio = require('cheerio');
request.post({
url: 'http://www.kicktipp.de/info/profil/loginaction',
headers: { 'content-type': 'application/x-www-form-urlencoded' },
method: 'post',
jar: jar,
body: 'kennung=test@example.com&passwort=1234567890&_charset_=UTF-8&submitbutton=Anmelden'
}, function(err, res, body){
if(err) {
return console.error(err);
};
request.get({
url: 'http://www.kicktipp.de/',
method: 'get',
jar: jar
}, function(err, res, body) {
if(err) {
return console.error(err);
};
var $ = cheerio.load(body);
var text = $('.dropdownbox > …Run Code Online (Sandbox Code Playgroud) 我正在使用mapCheerio结果列表来返回属性值.我想要的是一个包含属性值列表的变量(在本例中为ID),但我得到的是ID和额外的数据.
以下代码打印ID列表:
let ids = $('[data-profileid]').map(function() {
console.log($(this).attr('data-profileid'))
})
Run Code Online (Sandbox Code Playgroud)
结果:
1012938412
493240324
123948532
423948234
...
Run Code Online (Sandbox Code Playgroud)
但是,以下代码返回ID,但格式不同:
let ids = $('[data-profileid]').map(function() {
return $(this).attr('data-profileid')
})
console.log(ids)
Run Code Online (Sandbox Code Playgroud)
结果:
...
'69': '234234234,
'70': '9328402397432',
'71': '1324235234',
options:
{ withDomLvl1: true,
normalizeWhitespace: false,
xmlMode: false,
decodeEntities: true },
_root:
{ '0':
{ type: 'root',
name: 'root',
attribs: {},
...
Run Code Online (Sandbox Code Playgroud)
什么是这些额外的数据?当然不是必需的.我宁愿只有一个普通的数组.
我有两个文件; server.js和scrape.js,下面是他们目前的代码片段.
server.js:
const scrape = require("./scrape");
async function start() {
const response = await scrape.start();
console.log(response);
}
start();
Run Code Online (Sandbox Code Playgroud)
和scrape.js:
const cheerio = require("cheerio");
const request = require("request-promise");
go = async () => {
const options = {
uri: "http://www.somewebsite.com/something",
transform: function(body) {
return cheerio.load(body);
}
};
request(options)
.then($ => {
let scrapeTitleArray = [];
$(".some-class-in-html").each(function(i, obj) {
const data = $(this)
.text()
.trim();
scrapeTitleArray.push(data);
});
return scrapeTitleArray;
})
.catch(err => {
console.log(err);
});
};
module.exports = {
start: go …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用以下代码废弃网站:
const cheerio = require('cheerio');
const jsonframe = require('jsonframe-cheerio');
const $ = cheerio.load('https://coinmarketcap.com/all/views/all/');
jsonframe($); // initializes the plugin
//exception handling
process.on('uncaughtException', err =>
console.error('uncaught exception: ', err))
process.on('unhandledRejection', (reason, p) =>
console.error('unhandled rejection: ', reason, p))
const frame = {
"crypto": {
"selector": "tbody > tr",
"data": [{
"name": "td:nth-child(2) > a:nth-child(3)",
"url": {
"selector": "td:nth-child(2) > a:nth-child(3)",
"attr": "href"
},
"marketcap": "tr > td:nth-child(4)",
"price": "tr > td:nth-child(5) > a:nth-child(1)",
}]
}
};
let companiesList = $('tbody').scrape(frame);
console.log(companiesList);
Run Code Online (Sandbox Code Playgroud)
但是,我 …
cheerio ×10
node.js ×9
javascript ×6
web-scraping ×2
async-await ×1
asynchronous ×1
cookies ×1
encoding ×1
html ×1
jquery ×1
local ×1
meteor ×1
npm ×1
request ×1