['http://www.1.com', 'http://www.2.com', 'http://www.3.com']我想从以下字符串中获取。
const cheerio = require('cheerio')
const htmlStr = `
<div>
  <div class="item">
    <a href="http://www.1.com"></a>
  </div>
  <div class="item">
    <a href="http://www.2.com"></a>
  </div>
  <div class="item">
    <a href="http://www.3.com"></a>
  </div>
</div>
`
const $ = cheerio.load(htmlStr)
Run Code Online (Sandbox Code Playgroud)
最初,我认为$(div.item a)会返回一个元素数组。所以我尝试了:
const urls = $('div.item a').map(x => x.attr('href'))
Run Code Online (Sandbox Code Playgroud)
它失败了。
似乎$('div.item a')返回一个object.
怎么做?
谢谢!
提前澄清一下,我没有 Facebook 帐户,也无意创建一个帐户。另外,我想要实现的目标在我的国家和美国是完全合法的。
我不想使用 Facebook API 获取 Facebook 页面的最新时间线帖子,而是直接向页面 URL(例如this page)发送 get 请求并从 HTML 源代码中提取帖子。
(我想获取帖子的文字和创建时间。)  
当我在 Web 控制台中运行此命令时:
document.getElementsByClassName('userContent')
Run Code Online (Sandbox Code Playgroud)
我得到一个包含最新帖子文本的元素列表。
但我想从 Node.js 脚本中提取该信息。我可能可以使用无头浏览器等轻松完成此操作puppeteer,但这会产生大量不必要的开销。我真的很想要一种简单的方法,比如下载 HTML 代码,将其传递给 Cheerio 并使用 Cheerio 的类似 jQuery 的 API 来提取帖子。
这是我的尝试:
// npm i request cheerio request-promise-native
const rp = require('request-promise-native'); // requires installation of `request`
const cheerio = require('cheerio');
rp.get('https://www.facebook.com/pg/officialstackoverflow/posts/').then( postsHtml => {
    const $ = cheerio.load(postsHtml);
    const timeLinePostEls = $('.userContent');
    console.log(timeLinePostEls.html()); // should NOT be null
    const newestPostEl = timeLinePostEls.get(0); …Run Code Online (Sandbox Code Playgroud) 在这里,我有一个数组列表我的 URL 来自数组
网址:
url= result[urls]
console.log(url)
Run Code Online (Sandbox Code Playgroud)
输出:
books.toscrape.com/catalogue/a-light-in-the-attic_1000/index.html
Run Code Online (Sandbox Code Playgroud)
当我使用这个时:
 await page.goto('"http://' + url + '"');
Run Code Online (Sandbox Code Playgroud)
我收到类似这样的错误:
 Error: Protocol error (Page.navigate): Cannot navigate to invalid URL
Run Code Online (Sandbox Code Playgroud)
注意:当我用这个代替时:
await page.goto("http://books.toscrape.com/catalogue/soumission_998/index.html");
Run Code Online (Sandbox Code Playgroud)
然后就可以了
有人对此有想法吗?如何解决这个错误:)
当使用 Enzyme 在 React 中运行测试时,出现以下错误:
“错误:包子路径 './lib/utils' 不是由 ...\node_modules\cheerio\package.json 中的“exports”定义的。”
Cheerio 是 Enzyme 的依赖项。有问题的文件确实按预期存在于 node_modules 目录中。这个错误似乎是在将 Node.js 从 12.16.1 本地更新到 16.15.1 时开始的。
我正在运行:React:17.0.2 Enzyme:13.11.0 Node.js:16.15.1
当我在我的项目中使用 typescript 使用 Cheerio 时,并尝试通过 tsc 编译它。编译器抛出如下异常:
error TS2307: Cannot find module '.' or its corresponding type declarations.
2 import type { CheerioAPI, Cheerio } from '.'; 
Found 1 error in node_modules/cheerio/lib/esm/static.d.ts:2
Run Code Online (Sandbox Code Playgroud)
这似乎是包本身的错误。我应该如何解决这个问题?谢谢。
我只在我的项目中使用cheerio,如下所示:
import * as cheerio from "cheerio";
cheerio.load(content) // The type of content is string.
Run Code Online (Sandbox Code Playgroud)
"cheerio": "^1.0.0-rc.12""@types/cheerio": "^0.22.31"node v16.16.0npm v8.16.0
我正在使用请求和cheerio节点模块来创建从网站获取一些数据.我想得到一个项目列表,一旦这个列表完成,调用一个异步函数:
request('http://myurl', function(req,res,data){
    var $ = cheerio.load(data);
    var List = [];
    $('.myItems').each(function(i, element){
        console.log( typeof $(this).text() )
        List.push($(this).text());
    });
   for (var i=0; i <  List.length; i++){
      // make an asynchronous call to a API
   }
});
Run Code Online (Sandbox Code Playgroud)
我的问题是如何等待列表完成,即,我怎么知道.each函数已遍历所有项目?
我能用异步做到这一点吗?
谢谢
使用下面的HTML,我试图提取每个段落的html.但是,我找不到任何方法将节点转回HTML或查询对象.
以下是一个字符串 var html = ...
<article>
    <p> p1 </p>
    <p> p2 </p>
</article>
Run Code Online (Sandbox Code Playgroud)
html就这样加载了
var $ = require('cheerio').load(html)
var paragraphs = $('p').toArray().map(p => /* I want the html at this point */ )
Run Code Online (Sandbox Code Playgroud)
如何获取这些段落的HTML?
注意:为清楚起见,我调用cheerio.load"查询对象" 的返回值并返回toArray方法DOM节点; 缺乏一个更好的短语.
我想将sources配置对象的数组传递给jwplayer("vplayer").setup使用cheerio或其他模块.
<HTML>
<HEAD>
    <link rel="stylesheet" type="text/css" href="http://thevideos.tv/css/main.css">
    <script language="JavaScript" type="text/javascript" CHARSET="UTF-8"
            src="http://thevideos.tv/js/jquery.min.js"></script>
</HEAD>
<BODY topmargin=0 leftmargin=0 style="background:transparent;">
<table cellpadding=0 cellspacing=0>
    <tr>
        <td valign=top>
            <div style="position:relative;width:728px;height:410px;">
                <div id="play_limit_box">
                    <a href="http://thevideos.tv/premium.html" target="_blank">Upgrade you account</a> to watch videos
                    with no limits!
                </div>
                <span id='vplayer'><img src="http://192.99.62.187/i/01/00077/u0mqgq67qz76.jpg"
                                        style="width:728px;height:410px;"></span>    
            </div>
        </td>
    </tr>
</table>
<script type='text/javascript'>    jwplayer("vplayer").setup({
    sources: [{
        file: "http://192.99.62.187/kj2vyrxjey6vtaw52apz4kuggj6xfcc27pjizr5rhnrcgv73id7wwhzxlqda/v.mp4",
        label: "240p"
    }, {
        file: "http://192.99.62.187/kj2vyrxjey6vtaw52apz4kuggj6xfcc27pjizr5rhfbsgv73id76twjcd2ha/v.mp4",
        label: "360p"
    }]
});
</script>
<script>
    var sid = 90446;
    var wid = 115535;
</script>
</BODY> …Run Code Online (Sandbox Code Playgroud) 在Cheerio中,如何仅获取当前节点的文本?
var cheerio = require('cheerio')
const htmlString = '<div>hello<span>world</span></div>'
$ = cheerio.load(htmlString, { ignoreWhitespace: true })
console.log($('div').text())  //helloworld
console.log($('span').text())  //world
Run Code Online (Sandbox Code Playgroud)
你如何得到公正hello?
在处理网络抓取项目时,我无法以统一的方式获取某些数据。该页面有一个双列表,我只需要抓取第二列的文本即可运行编译值。
我正在这样做:
const rq = require('request');
const cheerio = require('cheerio');
rq(url, (err, res, html) => {
    let $ = cheerio.load(html);
    $('#table-id > tbody > tr > td.data').toArray().map(item => {
        console.log(item.text());
    });
});
Run Code Online (Sandbox Code Playgroud)
但是我收到一个.text()不是函数的错误。
cheerio ×10
node.js ×7
javascript ×6
web-scraping ×2
app-secret ×1
asynchronous ×1
enzyme ×1
es6-modules ×1
facebook ×1
html ×1
jquery ×1
npm ×1
package ×1
puppeteer ×1
regex ×1
typescript ×1