9gag 中文文档教程

发布于 4年前 浏览 79 项目主页 更新于 3年前

9gag

用于下载 9GAG 帖子和评论的 Node.js 包。

它可以作为一个模块来收集模因或作为命令行应用程序将数据保存到本地文件夹并生成 HTML 文件以供离线使用。

由于使用 Async/Await,需要 Node.js 8 或更高版本。

Install

npm install 9gag

Examples

CLI

9gag <post_count> [<folder> default:output] [<section> default:hot] [<comment_count> default:0]
9gag 10
9gag 20 output trending 3

Testing

# unit
npm run test:unit
# unit + integration
npm test

Scrap posts and comments with images

const NineGag = require('9gag');
const Scraper = NineGag.Scraper;
const Downloader = NineGag.Downloader;

async function memes() {
    try {
        // number of posts, section and number of comments
        // can pass a custom http client as the last Scraper argument
        const scraper = new Scraper(10, 'hot', 3);
        const posts = await scraper.scrap();
        posts.forEach(p => console.log(`${p.title} -> ${p.content} -> ${p.comments.map(c => c.content)[0]}`));
        await new Downloader('output').downloadPosts(posts);
    }
    catch (err) {
        console.error(err);
    }
}

memes();

HTML output

演示

9gag

A Node.js package to download 9GAG posts and comments.

It can be used as a module to scrap memes or as a command line application to save data to a local folder and generate an HTML file for offline use.

Node.js 8 or above required due to Async/Await usage.

Install

npm install 9gag

Examples

CLI

9gag <post_count> [<folder> default:output] [<section> default:hot] [<comment_count> default:0]
9gag 10
9gag 20 output trending 3

Testing

# unit
npm run test:unit
# unit + integration
npm test

Scrap posts and comments with images

const NineGag = require('9gag');
const Scraper = NineGag.Scraper;
const Downloader = NineGag.Downloader;

async function memes() {
    try {
        // number of posts, section and number of comments
        // can pass a custom http client as the last Scraper argument
        const scraper = new Scraper(10, 'hot', 3);
        const posts = await scraper.scrap();
        posts.forEach(p => console.log(`${p.title} -> ${p.content} -> ${p.comments.map(c => c.content)[0]}`));
        await new Downloader('output').downloadPosts(posts);
    }
    catch (err) {
        console.error(err);
    }
}

memes();

HTML output

demo

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文