All Projects → justjavac → deno_cheerio

justjavac / deno_cheerio

Licence: MIT license
How to use cheerio in Deno

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to deno cheerio

deno install
Deno 安装器(国内加速)
Stars: ✭ 58 (+152.17%)
Mutual labels:  deno, deno-modules
oembed-parser
Extract oEmbed data from given webpage
Stars: ✭ 65 (+182.61%)
Mutual labels:  deno
Deno
A modern runtime for JavaScript and TypeScript.
Stars: ✭ 79,289 (+344634.78%)
Mutual labels:  deno
discord-emoji
[Library/Deno] A near exact emoji tables of Discord for string-based insertion of emotes without having to escape Unicode.
Stars: ✭ 37 (+60.87%)
Mutual labels:  deno
superdeno
Super-agent driven library for testing Deno HTTP servers.
Stars: ✭ 119 (+417.39%)
Mutual labels:  deno
awesome-oak
A list of community projects for oak
Stars: ✭ 63 (+173.91%)
Mutual labels:  deno
Aleph.js
The Full-stack Framework in Deno.
Stars: ✭ 3,448 (+14891.3%)
Mutual labels:  deno
dotland
deno.land website
Stars: ✭ 947 (+4017.39%)
Mutual labels:  deno
Thread
type safe multi-threading made easier
Stars: ✭ 34 (+47.83%)
Mutual labels:  deno
i18next-fs-backend
i18next-fs-backend is a backend layer for i18next using in Node.js and for Deno to load translations from the filesystem.
Stars: ✭ 67 (+191.3%)
Mutual labels:  deno
media types
Deprecated. Use std/media_types instead.
Stars: ✭ 21 (-8.7%)
Mutual labels:  deno
dmm
Lightweight Deno Module Manager
Stars: ✭ 59 (+156.52%)
Mutual labels:  deno
deno sticker
🦕 The data I used for submitting for printing deno_sticker.
Stars: ✭ 50 (+117.39%)
Mutual labels:  deno
ptera
Ptera is DateTime library for Deno
Stars: ✭ 62 (+169.57%)
Mutual labels:  deno
Fae
A functional module for Deno inspired from Ramda.
Stars: ✭ 44 (+91.3%)
Mutual labels:  deno
Zip.js
JavaScript library to zip and unzip files in the browser and Deno
Stars: ✭ 2,444 (+10526.09%)
Mutual labels:  deno
astrodon
Make Desktop apps with Deno 🦕
Stars: ✭ 826 (+3491.3%)
Mutual labels:  deno
denoliver
A simple, dependency free static file server for Deno with possibly the worst name ever.
Stars: ✭ 94 (+308.7%)
Mutual labels:  deno
zeno.zsh
zsh fuzzy completion and utility plugin with Deno.
Stars: ✭ 119 (+417.39%)
Mutual labels:  deno
azure-functions-deno-worker
Run Deno 🦕 on Azure Functions ⚡️
Stars: ✭ 99 (+330.43%)
Mutual labels:  deno

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].