V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
sead
V2EX  ›  分享创造

基于 cheerio, 写了个低代码,高效解析轮子

  •  
  •   sead ·
    seadfeng · 52 天前 · 2336 次点击
    这是一个创建于 52 天前的主题,其中的信息可能已经有所发展或是发生改变。

    低代码,高效而简洁;支持正则表达式,复杂的场景也能很容易提取数据。

    • 支持: Nodejs, TypeScript
    • 依赖: Cheerio, turndown
    • HTML 代码片段可以转 Markdown

    使用 YAML 来配置解析,可高效管理输出 JSON 结构

    以下是最简单的解析形态,有个更复杂 SERP 例子在 git 中可作为学习参考。

    YAML 配置

    tree:
      nodes:
        title:
          selector: title
        body:
          selector: body
          attr: html
          to_markdown: true
    

    HTML

    <html lang="en">
      <head>
        <title>Cheerio Tree</title>
      </head>
      <body>
        <h1>Cheerio Tree</h1>
        <main>
          <h2>What is Cheerio Tree?</h2>
          <p><b>Cheerio Tree</b> is a powerful utility built on <b>Cheerio</b>, designed for efficient DOM parsing. It enables rapid conversion of HTML data into JSON format. When paired with YAML, it provides an intuitive and streamlined approach to data handling and transformation.</p>
        </main>
      </body>
    </html>
    

    输出

    {
      "title":"Cheerio Tree",
      "body":"Cheerio Tree\n============\n\nWhat is Cheerio Tree?\n---------------------\n\n**Cheerio Tree** is a powerful utility built on **Cheerio**, designed for efficient DOM parsing. It enables rapid conversion of HTML data into JSON format. When paired with YAML, it provides an intuitive and streamlined approach to data handling and transformation."
    }
    

    传输门: https://github.com/serping/cheerio-tree

    第 1 条附言  ·  50 天前

    帖子要沉了,社区玩采集的不多?

    你们都是怎么写代码和维护的;

    现在我撸数据只需要关注yaml, Git 中有Google的SERP片段, 复杂一点的需求用例。

    详细文档需要时间,陆续会出一些 API scraper 采集规则,YAML配置例子更直观。

    11 条回复    2024-07-24 15:32:52 +08:00
    Amose2024
        1
    Amose2024  
       52 天前
    顶一个。不知道与 beautifulSoup 有何不同?
    sead
        2
    sead  
    OP
       51 天前
    @Amose2024 cheerio 和 beautifulSoup 解决的需求都差不多,但是多数场景都是一些重复的数据处理逻辑,维护起来也是比较头大;目前用这个撸 serp 暂时还没碰到解决不了的.

    https://serpchecking.com/ 完全基于 Cheerio-Tree 撸
    kiroli
        3
    kiroli  
       50 天前
    用 cheerio 指哪打哪,感觉更直接些
    sead
        4
    sead  
    OP
       50 天前
    @kiroli 写原生? 可能是不会设计代码结构,数据复杂时维护起来很痛苦。。之前用 python 撸了一个 amazon asin 铲子,数据过于复杂,维护起来头大。 基于痛点才撸了这个。
    good1uck
        5
    good1uck  
       50 天前
    没 DEMO 看不懂
    sead
        6
    sead  
    OP
       50 天前
    @good1uck 文档和 demo 后续会更新,近几天基于这个会发布一个 express api 辅助开发,同时可以用于生产环境的 api ,会有更新详细的 demo 。
    sead
        7
    sead  
    OP
       48 天前
    @good1uck https://github.com/serping/express-scraper DEMO 发布了,目前有三个 API 例子。

    这个是 expressjs api , 扩展非常方便
    sead
        8
    sead  
    OP
       48 天前
    @Amose2024 https://github.com/serping/express-scraper 基于轮子的 API 应用版本,线上部署了。

    其中 SERP 的较为复杂,不过按树结构方法解析,应该还是比较容易看懂的。
    sead
        9
    sead  
    OP
       48 天前
    @kiroli https://github.com/serping/express-scraper 可以参考下这种方式怎样?里面有很直观的例子。
    InsideSeed
        10
    InsideSeed  
       45 天前
    已 star ,看着挺不错的,但是目前似乎没有完整的文档
    sead
        11
    sead  
    OP
       45 天前
    @InsideSeed 有一个 SVG 结构图,后续我会发布文档和一些常见场景的用法
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1138 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 23:46 · PVG 07:46 · LAX 16:46 · JFK 19:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.