9gscraper 中文文档教程

发布于 5年前 浏览 41 更新于 3年前

9gscraper

用于抓取 9gag 帖子和评论的 nodejs 包。

Documentation

getFeed([options])

  • options {Object} 一个包含:section、batchSize 和 limit 的对象。

  • section {String} 栏目类型,默认:trending

  • batchSize {Number} 表示刮板一次应该在缓冲区中有多少帖子,默认值:10。

  • limit {Number} 结果总数,默认值:-1。

  • 返回 post 对象的异步迭代器。

post 对象示例:

{
  "id": "aQ1edyK",
    "url": "https://9gag.com/gag/aQ1edyK",
    "title": "Keep the culture alive",
    "content": {
        "type": "video",
        "src": [
            {
                "src": "https://img-9gag-fun.9cache.com/photo/aQ1edyK_460svvp9.webm",
                "format": "video/webm"
            },
            {
                "src": "https://img-9gag-fun.9cache.com/photo/aQ1edyK_460sv.mp4",
                "format": "video/mp4"
            },
            {
                "src": "https://img-9gag-fun.9cache.com/photo/aQ1edyK_460svwm.webm",
                "format": "video/webm"
            }
        ]
    },
    "category": "GIF",
    "comments": "Object [AsyncGenerator]"
}

Examples

使用 Promises 和 forEach 获取前 1k 个帖子

const scraper = require("9gscraper");
scraper.getFeed({ limit: 1000 }).then(async feed => {
  await feed.forEach(post=>console.log(post.title));
});

使用 Async/Await 和 for 循环从“hot”部分的前 5 个帖子中获取评论

const scraper = require("9gscraper")
(async () => {
  const feed = await scraper.getFeed({ section: "hot", limit: 5 });
  for await (const post of feed) {
    for await (const comment of post.comments) {
      console.log(`${comment.user.displayName}: ${comment.text}`);
    }
  }
})();

9gscraper

A nodejs package to scrape 9gag posts and comments.

Documentation

getFeed([options])

  • options {Object} an object containing: section, batchSize and limit.

  • section {String} sections type, default: trending.

  • batchSize {Number} indicates how many posts should the scraper have in the buffer at one time, default: 10.

  • limit {Number} total count of results, default: -1.

  • returns asynchronous iterator of post objects.

post object example:

{
  "id": "aQ1edyK",
    "url": "https://9gag.com/gag/aQ1edyK",
    "title": "Keep the culture alive",
    "content": {
        "type": "video",
        "src": [
            {
                "src": "https://img-9gag-fun.9cache.com/photo/aQ1edyK_460svvp9.webm",
                "format": "video/webm"
            },
            {
                "src": "https://img-9gag-fun.9cache.com/photo/aQ1edyK_460sv.mp4",
                "format": "video/mp4"
            },
            {
                "src": "https://img-9gag-fun.9cache.com/photo/aQ1edyK_460svwm.webm",
                "format": "video/webm"
            }
        ]
    },
    "category": "GIF",
    "comments": "Object [AsyncGenerator]"
}

Examples

Get first 1k posts using Promises and forEach

const scraper = require("9gscraper");
scraper.getFeed({ limit: 1000 }).then(async feed => {
  await feed.forEach(post=>console.log(post.title));
});

Get comments from the first 5 posts in section "hot" using Async/Await and for loop

const scraper = require("9gscraper")
(async () => {
  const feed = await scraper.getFeed({ section: "hot", limit: 5 });
  for await (const post of feed) {
    for await (const comment of post.comments) {
      console.log(`${comment.user.displayName}: ${comment.text}`);
    }
  }
})();
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文