我有几个实用函数,可以对 Cheerio 对象进行操作。对于几乎每一个函数,我都必须将 $ 与元素本身一起传递。
例子:
function aUtilityFunc($, cheerioEl) { // <- $ in the params
    return cheerioEl.each(function (i, child) {
        // i do not want to do this:
        $(child).attr("something", $(child).attr("something") + "something");
        // i would rather do this and omit the $ in the params (like with global jquery doc):
        var $ = cheerioEl.$;
        $(child).attr("something", $(child).attr("something") + "something");
    });
}
Run Code Online (Sandbox Code Playgroud)
对于这个问题是否有一个优雅的解决方案,允许我只将 1 个参数传递给我的函数?(我并不是说将它们包装到对象文字中:>)。因为坦率地说,这种方式不太好(除非我忽略了某些事情)。
我正在从页面中提取元标记:
$ = cheerio.load(html)
metaTags = $('meta')
Run Code Online (Sandbox Code Playgroud)
它工作正常,但我需要metaTags数组包含字符串 - 而不是 cherrio 的对象,如下所示:
[“<'meta sometag=1231'><'/meta'>”,“<'meta sometag=44242'><'/meta'>”]
ps 我不需要 ' 字符,它只是 stackoverflow.com 的误解
我做了这样的方法:
toHtml = (el) ->
  return el.html()
Run Code Online (Sandbox Code Playgroud)
但它不起作用:得到空结果(使用地图)
metaTags.map (i, el) -> console.log i.toHtml(el)
基本上我正在尝试解析 HTML 字符串并使用Cheerio.js提取一些信息。
我的 HTML 如下(当然我减少并简化了它):
<html>
    <head></head>
    <body>
        <div>
            <table>
                <tr>
                    <td>
                        <a href="/link_1.php">Link 1</a>
                    </td>
                    <td>
                        <a href="/link_2.php">Link 2</a>
                        <a href="/link_3.php">Link 3</a>
                    </td>
                    <td>
                        <a href="/link_4.php">Link 4</a>
                        <a href="/link_5.php">Link 5</a>
                    </td>
                </tr>
            </table>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)
我的代码是这样的:
var cheerio = require("cheerio");
var $ = cheerio.load(html);
var page = $.root();
var tr = page.find("tr");
console.log(tr.find("> :nth-child(2) a").length);
Run Code Online (Sandbox Code Playgroud)
你可以在这里尝试一下。
我期望的是返回的代码,2因为该元素的第二个直接子元素中有两个链接tr。但是,这将返回5,返回 中的所有链接tr。
我用 jQuery 尝试了同样的事情,结果应该是这样,请参阅。
我还注意到删除<html>标签可以使其正常工作,但我不知道为什么。 …
尝试运行代码时,我不断收到错误$.find('.market_listing_item_name_block').each()- undefined 不是函数,指向 find。我认为 find 是cheerio中的一个函数?公平地说,我不确定我是否做对了,这是我的代码:
var cheerio = require('cheerio')
$ = cheerio.load('#searchResultsRows')
var url = 'http://steamcommunity.com/market/search?appid=730'
xhr.get(url).success(function(r){
    $.find(".market_listing_item_name_block").each(function(){
        var name = $(this).find(".market_listing_item_name").text();
        console.log(name)
    })
})
Run Code Online (Sandbox Code Playgroud)
xhr 是一个本质上类似于 AJAX 的对象。
我之前在 chrome 中的做法是:
var url = 'http://steamcommunity.com/market/search?appid=730'
var itemDiv = $("<div></div>")
$.get(url).success(function(r){
    d = $(r).find('#searchResultsRows')
    itemDiv.append(d)
})
Run Code Online (Sandbox Code Playgroud)
进而:
itemDiv.find(".market_listing_item_name_block").each(function(){
   var name = $(this).find(".market_listing_item_name").text();
   console.log(name) // would normally do other things, but for the purpose of this post, i'm just console logging the name
})
Run Code Online (Sandbox Code Playgroud)
我究竟如何才能在 node/cheerio 中重新创建那个 ^?我相信我显然错过了几步。非常感谢任何帮助,谢谢。
此 Meteor 服务器端代码试图获取此 html 字符串中的数量 77,但我的选择器返回空对象。我怎样才能从这个 html 中得到 77?谢谢
$('select[name=paid] option').data();
Run Code Online (Sandbox Code Playgroud)
<td class="displayValue">
            <select name="paid" id="paidId"><option value="77.00" selected="selected">77.00</option>
              <option value="Other">Other</option></select>
      </td>
    </tr>
Run Code Online (Sandbox Code Playgroud) 概括:
我目前正在将 Apache + PHP 堆栈上的网站迁移到 Node + Express,并且想知道在新堆栈下动态注入元标记的最佳方法/最佳实践(如果有的话)是什么。
细节:
在现有堆栈下,通过直接将 PHP 代码添加到 HTML 文件中来动态注入元标记。由于渲染是在服务器端完成的,因此 Facebook/Google+/任何网络爬虫都会正确解释标签。
在新的堆栈下,经过一些研究后,我遇到了两种选择:
在这两种选择之间,哪一种具有更好的性能或者没有实质性差异?您还有其他推荐的方法吗?谢谢!
我是 JavaScript 的新手,我很确定我在从 HTML 页面(由网络浏览器浏览)使用 JS 时遗漏了一些基本的东西。
我的目标是报废的照片链接从一个动态的网站使用cheerio并显示它们一个js的小工具(例如,使用lightslider),它看起来相当成功按照本教程以获得下面的脚本并运行它通过简单的NodeJS scrapt.js在bash终端:
var request = require('request');
var cheerio = require('cheerio');
request('https://outbox.eait.uq.edu.au/uqczhan2/Photos/', function (error, respo
  if (!error && response.statusCode == 200) {
    console.log(html);
  }
});
Run Code Online (Sandbox Code Playgroud)
但是现在我无法在通用网络浏览器中运行此脚本(按 f12 -> 控制台),因为第一个语法后显示错误:
>var request = require('request');
VM85:1 Uncaught ReferenceError: require is not defined
    at <anonymous>:1:15
Run Code Online (Sandbox Code Playgroud)
我知道在使用之前需要加载一些 JavaScript 模块,例如 d3.js。我需要运行:
<script src="https://d3js.org/d3.v4.min.js"></script>
Run Code Online (Sandbox Code Playgroud)
使用所有 d3 功能。我应该如何实现让我在 Web 浏览器中使用cheerio 的相同功能?
  let playersCell = `
    <td class="foo" colspan="2">
      <a href="example.com">
        <span class="bold">John Beluga</span>
         - Sarah Jay.
       </a>
    </td>
    `
let players = cheerio.load(playersCell)
players.find('a').html()
Run Code Online (Sandbox Code Playgroud)
我尝试将 html 字符串加载到 Cheerio.js 中并找到一个a标签,但我得到了
[类型错误:players.find 不是函数]
Console.log显示为players
尝试使用 Cheerio 拉取 img 源,但 img 没有类。看起来像
<div class="container_c89a5 lazyLoadContainer_b1038">
<img height="80" src="https://stuff.com" srcset="https://stuff.com" width="80">
</div>
Run Code Online (Sandbox Code Playgroud)
我试过以几种不同的方式选择图像源,但没有运气。
var $ = cheerio.load(html);
    $('div.item_54fdd').each(function(i, element) {
        var a = $(this);
        var title = a.find('.title_9ddaf').text(); //works great
        var image = a.find('div.container_c89a5').first('img').attr('src');  //no luck
        var image = a.find('div.container_c89a5 > img').attr('src');  //no luck
Run Code Online (Sandbox Code Playgroud) 我们有几个旧站点正在进行升级。如果能够对每个页面进行屏幕截图,然后对两个域的结果进行 md5 求和,然后测试呈现的所有内容是否 100% 匹配,这将非常有用。
我不确定如何做到这一点 - 我们已经研究了cheerio哪些可以抓取网站但无法抓取屏幕截图,以及 nightwatch 可以抓取屏幕截图但不能抓取网站。有人有这样做的经验吗?
cheerio ×10
javascript ×7
node.js ×6
jquery ×3
html ×2
coffeescript ×1
express ×1
function ×1
parsing ×1
phantomjs ×1
pug ×1
web-crawler ×1