我有以下html,我想通过Cheerios解析.
    var $ = cheerio.load('<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>This works well.</div><div><br clear="none"/></div><div>So I have been doing this for several hours. How come the space does not split? Thinking that this could be an issue.</div><div>Testing next paragraph.</div><div><br clear="none"/></div><div>Im testing with another post. This post should work.</div><div><br clear="none"/></div><h1>This is for test server.</h1></body></html>', {
    normalizeWhitespace: true,
});
// trying to parse the html
// the goals are to 
// 1. remove all the 'div'
// 2. clean up …Run Code Online (Sandbox Code Playgroud) 我正在寻找一个与jQuery或Cheerio具有类似API和用法的库.
我的用例是:解析包含javascript/css文件引用的任何脚本或链接标记的HTML文件.
我正在尝试使用Node.js和Cheerio解析一个HTML表格并得到一些结果但不幸的是我得到了太多的数据,我不知道如何进一步解析它以获得我需要的数据.
这是我到目前为止的一小段代码..
var request = require("request");
var cheerio = require("cheerio");
request('http://www.myURL.com', function(error, response, body) {
  var $ = cheerio.load(body);
  $('td').each(function() {
    console.log($(this).text());
  });
});
Run Code Online (Sandbox Code Playgroud)
使用Chrome插件找到选择器,我发现我需要".clickableRow td"但是我试图插入它的每一种方式似乎都不起作用.
为了更清晰一点,html源代码如下所示 -
<html>
 <body>
  <form>
   <table>
    <tbody>
     <td>
      <table class="standardTable">
       <tbody>
        <tr class="clickableRow">
         <td>first thing I want</td>
         <td>second thing I want</td>
         <td>third thing I want</td>
         <td>fourth thing I want</td>
Run Code Online (Sandbox Code Playgroud)
那有意义吗?我想要的项目非常深入HTML,我不知道如何达到这个水平.任何帮助将不胜感激!谢谢!
此 Meteor 服务器代码尝试使用包从 html 字符串中提取innerHTML cheerio,但错误表明elements没有方法“size”
我做错了什么以及如何解决?谢谢
这是 html;
<span class='errorMsg'>some text </span>
message: (html, typeMsg) => {
      let $ = cheerio.load(html);
      const selection = 'span.' + typeMsg;
      const elements = $(selection);
      return elements.size() > 0 ? elements.get(0).innerHTML.trim() : '';
    }
Run Code Online (Sandbox Code Playgroud) 我是 Javascript 的新手,想使用 Cheerio 库来做一些网页抓取。在图书馆介绍中看到了这段文字。我不确定选择器、上下文和根之间有什么区别。
Cheerio 的选择器实现与 jQuery 几乎相同,因此 API 非常相似。
$( 选择器, [上下文], [根] )
选择器在上下文范围内搜索,在根范围内搜索。选择器和上下文可以是字符串表达式、DOM 元素、DOM 元素数组或cheerio 对象。root 通常是 HTML 文档字符串。
这个选择器方法是遍历和操作文档的起点。与 jQuery 一样,它是在文档中选择元素的主要方法,但与 jQuery 不同的是,它构建在 CSSSelect 库之上,该库实现了大多数 Sizzle 选择器。
示例 API:
<ul id="fruits">
  <li class="apple">Apple</li>
  <li class="orange">Orange</li>
  <li class="pear">Pear</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
$('.apple', '#fruits').text() //=> 苹果
$('ul .pear').attr('class') //=> 梨
$('li[class=orange]').html() //=> 橙色
在第一个示例中,.apple 是选择器,#fruits 是上下文。那讲得通。在第二个例子中,ul 是选择器,.pear 是上下文吗?如果选择器是为了在上下文中搜索,那么鉴于 .pear 嵌套在 ul 中,这很奇怪?
选择具有特定属性的所有dom元素的最有效方法是什么.
<input name="mode">
Run Code Online (Sandbox Code Playgroud)
使用普通的javascript我会使用:document.querySelectorAll("[name='mode']")
或者document.querySelectorAll("[name]")如果我不关心属性值.
尝试运行代码时,我不断收到错误$.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) 我是 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 的相同功能?
尝试使用 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) cheerio ×10
node.js ×7
javascript ×5
jquery ×4
api ×1
dom ×1
html ×1
html-parsing ×1
meteor ×1
python ×1