Tag: cheerio

当使用Cheerio和NodeJS进行search时,Object#<Object>没有方法'attr'

当在Node JS中跟随一个关于网页抓取的教程时,我在terminal中运行这个脚本时遇到了这个问题: Object #<Object> has no method 'attr' 这是我正在使用的脚本: var request = require('request'), cheerio = require('cheerio'), urls = []; request('http://www.reddit.com', function (err, resp, body) { if (!err && resp.statusCode == 200) { var $ = cheerio.load(body); $('a.title', '#siteTable').each(function() { var url = this.attr('href'); urls.push(url) }); console.log(urls); } }); 任何想法如何解决这个脚本,所以它不会抛出一个错误? 任何帮助将非常感激!

我可以依靠Cheerio以文件顺序退货吗?

我正在使用cheerio.js进行网页抓取。 它显示出与jquery有细微的差别,例如:eq在select器中不可用。 我想问一下经验,我是否可以信任cheerio以文件顺序退货。 例如,如果我有一个<ul><li></li><li></li></ul>并运行$('li').get(0) ,它会给我第一个列表项目。

Cheerio itemprop属性内容select

我在nodejs中使用Cheerio来从一个元素包含属性itemprop =“name”的URL中select文本。 此刻我需要知道父元素为了读取属性和相关文本。 以下面为例。 不过,我想要做的是为元素插入一个通配符。 例如。 H2,所以我可以select名称=“itemprop”的任何属性。 这可能吗? var $ = cheerio.load(body); var domElem = $("h2[itemprop = 'name']").get(0); var content = $(domElem).text().trim(); ogTitle = content; console.log(content);

如何在Node.js中高效地进行网页抓取?

我试图从购物网站Express.com刮取一些数据。 以下是许多含有图片 , 价格 , 标题 , 颜色的产品中的一种 。 <div class="cat-thu-product cat-thu-product-all item-1"> <div class="cat-thu-p-cont reg-thumb" id="p-50715" style="position: relative;"><a href="/rocco-slim-fit-skinny-leg-corduroy-jean-50715-647/control/show/3/index.pro" onclick="var x=&quot;.tl(&quot;;s_objectID=&quot;http://www.express.com/rocco-slim-fit-skinny-leg-corduroy-jean-50715-647/control/show/3/index.pro_1&quot;;return this.s_oc?this.s_oc(e):true"><img class="cat-thu-p-ima widget-app-quickview" src="http://t.express.com/com/scene7/s7d5/=/is/image/expressfashion/25_323_2516_900/i81?$dcat191$" alt="ROCCO SLIM FIT SKINNY LEG CORDUROY JEAN"></a><a href="#jsLink"><img id="widget-quickview-but" class="widget-ie6png glo-but-css-off2" src="/assets/images/but/cat/but-cat-quickview.png" alt="Express View" style="position: absolute; left: 50px;"></a></div> <ul> <li class="cat-cat-more-colors"> <div class="productId-50715"> <img class="js-swatchLinkQuickview" title="INK BLUE" src="http://t.express.com/com/scene7/s7d5/=/is/image/expressfashion/25_323_2516_900_s/i81?$swatch$" width="16" […]

Cheerio(node.js)在html读取时返回一个错误

我对JS完全陌生,完全停留在Node Cheerio上。 如果有人能帮助我,我将不胜感激。 我正在处理的代码在这里: https : //github.com/zafartahirov/bitstarter 。 (找不到的代码在find修复程序后会被调用grader.old.js)。 当我跑步 node ./grader.js –file index.html –checks checks.json | less 我收到一个错误: TypeError: Cannot call method 'utf8Slice' of null at Buffer.toString (buffer.js:416:26) at exports.isTag (/home/ubuntu/GitHub/bitstarter/node_modules/cheerio/lib/utils.js:15:14) at Function.filter (/home/ubuntu/GitHub/bitstarter/node_modules/cheerio/node_modules/lodash/dist/lodash.js:3150:15) at /home/ubuntu/GitHub/bitstarter/node_modules/cheerio/lib/api/traversing.js:9:26 at Function.reduce (/home/ubuntu/GitHub/bitstarter/node_modules/cheerio/node_modules/lodash/dist/lodash.js:3734:25) at exports.find (/home/ubuntu/GitHub/bitstarter/node_modules/cheerio/lib/api/traversing.js:8:17) at new module.exports (/home/ubuntu/GitHub/bitstarter/node_modules/cheerio/lib/cheerio.js:92:18) at initialize (/home/ubuntu/GitHub/bitstarter/node_modules/cheerio/lib/static.js:23:12) at checkHtmlFile (/home/ubuntu/GitHub/bitstarter/grader.js:59:23) at Object.<anonymous> […]

如何在每个()函数内运行asynchronous代码?

我需要能够在每个循环内运行asynchronous代码,而不是直到asynchronous代码完成。 $("#items").children(".block-option-small").each(function(item){ request(newurl,function(err, resp, body) { $ = cheerio.load(body);}); });

刮与cheerio的文本

我正在试图从这个html中刮掉Jung Ho Kang和5 ,并把它放到一个对象中。 我想排除(R)和SS 。 <td id="lineup-table-top"> <b class="text-muted pad-left-10">5</b> &nbsp;&nbsp;&nbsp;Jung Ho Kang <small class="text-muted">(R)</small> <small class="text-muted">SS</small> </td> 这是我的代码: var someObjArr = []; $('td#lineup-table-top').each(function(i, element){ //Get the text from cheerio. var text = $(this).text(); //if undefined, create the object inside of our array. if(someObjArr[i] == undefined){ someObjArr[i] = {}; }; //Update the salary property […]

jQuery获取除了子元素X之外的子元素的HTML

如何使用jQuery / Javascript来select第一个<div class="description中的两个<p>元素的HTML?Regex也很好,这个jQuery的select实际上是在一个cheerio对象的Node.js中完成的。 运用 $( $('.description')[0] ).children().not('h2').html() 似乎只抓文字 Foo Bar 代替 <p>Foo</p> <p>Bar</p> HTML: <div class='description'> <h2>Hello world</h2> <p>Foo</p> <p>Bar</p> </div> <div class='description'> <h2>Goodbye world</h2> <p>Didi</p> <p>Deedee</p> </div>

剥离每个子元素的html

让我说我有这样的肮脏的HTML … let dirty = ` <pre> pre tag with <b>html</b> </pre> <pre> another pre tag with <b>html</b> </pre> ` 我需要从每个孩子的标签剥离HTML … 即时通讯做这个… let $ = cheerio.load(dirty) $('pre').each(function() { let text = $(this).text() console.warn(text) // html are stripped return `<pre>${text}</pre>` }); console.log($.html()) // html are not stripped 我错过了什么?

Node.js + Cheerio:循环内的请求

我正在使用cheerio,请求和Node.js。 当我运行下面的脚本时,它以错误的顺序输出名称。 我相信它是由asynchronous性质引起的,我怎样才能使它在“正确”的顺序工作? 我是否需要使用同步软件包,或者是否有办法以某种方式更改它,以便以同步方式工作? app.get('/returned', function (req, res) { for (var y = 0; y < 10; y++) { var url = "http://example.com" + y + "/person.html"; request(url, function (err, resp, body) { $ = cheerio.load(body); var links = $('#container'); var name = links.find('span[itemprop="name"]').html(); // name if (name == null) { console.log("returned null"); } else […]