Git Product home page Git Product logo

deno_cheerio's Introduction

deno_cheerio

Build Status

记录一次在 Deno 中使用 cheerio 库的过程。

如何在 Deno 中使用 cheerio

cheerio 是一个非常流行的 npm 包,为服务器特别定制的,快速、灵活、实施的 jQuery 核心实现。可以说 cheerio 就是一个 Node.js 版的 jQuery。

那么我们在 Deno 中如何使用这个库呢?

使用

如果直接在 Deno 中使用源码,像这样:

import * as Cheerio from "https://raw.githubusercontent.com/cheeriojs/cheerio/v1.0.0/lib/cheerio.js";

会报错:

error: Uncaught ReferenceError: require is not defined
var parse = require('./parse'),
            ^
    at https://raw.githubusercontent.com/cheeriojs/cheerio/v1.0.0/lib/cheerio.js:6:13

因为 Deno 并不支持 commonjs 规范,只支持 esm。

因此我们必需借助 jspm.io(或其他类似服务)来将 commonjs 转换为兼容的 esm 格式。

我们可以这样:

import cheerio from "https://dev.jspm.io/npm:cheerio/index.js";

const $ = cheerio.load('<h2 class="title">Hello world</h2>');

$("h2.title").text("Hello Deno!");
$("h2").addClass("deno");

console.log($.html());

我们试着运行一下:

deno run mod.ts

成功输出了 <h2 class="title deno">Hello Deno!</h2>

添加 TypeScript 支持

好在 @types 仓库提供了 cheerio 的类型定义文件,我们在 mod.ts 顶部增加一行:

+// @deno-types="https://dev.jspm.io/@types/cheerio/index.d.ts"
 import cheerio from "https://dev.jspm.io/cheerio/index.js";

运行一下,又报错了

error: relative import path "node" not prefixed with / or ./ or ../ Imported 
from "https://dev.jspm.io/npm:@types/[email protected]/index.d.ts"

看来这个 d.ts 文件和 deno 不兼容,把这个文件下载到本地,新建 cheerio.d.ts 改造一下。

问题出在第 14 行,/// <reference types="node" /> 与 Deno 不兼容,于是删掉这一行:

-/// <reference types="node" />
-

再次运行,又报错:

error: TS2580 [ERROR]: Cannot find name 'Buffer'. Do you need to install type definitions for node? Try `npm i @types/node`.
  load(html: string | Buffer, options?: CheerioOptionsInterface): CheerioStatic;
                      ~~~~~~
    at https://cdn.jsdelivr.net/gh/justjavac/deno_cheerio/cheerio.d.ts:310:23

Buffer 是 nodejs 的类型,所以报错了。

其实 Deno 也有 Buffer,我们需要使用 Deno.Buffer 来引用,考虑到 Deno 的 Buffer 和 Node.js 的并不兼容,于是直接删掉这个类型。

(补充 2021-04-19,Deno 1.9 已经放弃了 Deno.Buffer,在 2.0 会将其移除)

-  load(html: string | Buffer, options?: CheerioOptionsInterface): CheerioStatic;
+  load(html: string, options?: CheerioOptionsInterface): CheerioStatic;

再次运行,终于得到了我们想要的结果:

<h2 class="title deno">Hello Deno!</h2>

例子

deno run https://cdn.jsdelivr.net/gh/justjavac/deno_cheerio/mod.ts

License

deno_cheerio is released under the MIT License. See the bundled LICENSE file for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.