当前位置: 首页 > news >正文

github项目--crawl4ai

github项目--crawl4ai

    • 输出html
    • 输出markdown格式
    • 输出结构化数据
    • 与BeautifulSoup的对比

crawl4ai github上这个项目,没记错的话,昨天涨了3000多的star,今天又新增2000star。一款抓取和解析工具,简单写个demo感受下

这里我们使用crawl4ai抓取github每日趋势,每天通过邮件发到自己邮箱

输出html

async def github_trend_html():async with AsyncWebCrawler(verbose=True) as crawler:result = await crawler.arun(url="https://github.com/trending",)assert result.success, "github 数据抓取失败"return result.cleaned_html

输出的还是html,但对原始页面做了处理,比如移除不相关元素,动态元素,简化html结构。

在这里插入图片描述

输出markdown格式

async def github_trend_md():async with AsyncWebCrawler(verbose=True) as crawler:result = await crawler.arun(url="https://github.com/trending",)assert result.success, "github 数据抓取失败"return result.markdown

用md软件打开看一下效果:

在这里插入图片描述

输出结构化数据

async def github_trend_json():schema = {"name": "Github trending","baseSelector": ".Box-row","fields": [{"name": "repository","selector": ".lh-condensed a[href]","type": "text",},{"name": "description","selector": "p","type": "text",},{"name": "lang","type": "text","selector": "span[itemprop='programmingLanguage']",},{"name": "stars","type": "text","selector": "a[href*='/stargazers']"},{"name": "today_star","type": "text","selector": "span.float-sm-right",},],}extraction_strategy = JsonCssExtractionStrategy(schema, verbose=True)async with AsyncWebCrawler(verbose=True) as crawler:result = await crawler.arun(url="https://github.com/trending",extraction_strategy=extraction_strategy,bypass_cache=True,)assert result.success, "github 数据抓取失败"github_trending_json = json.loads(result.extracted_content)for ele in github_trending_json:ele['repository'] = 'https://github.com/' + ''.join(ele['repository'].split())return github_trending_json

与前两种不同的是,结构化输出需要通过自定义schema来定义解析的数据结构。控制台按照我们定义的schema输出了标准了JSON数据。将数据放入html模版,通过邮件每日发送。看一下邮件显示:

在这里插入图片描述

与BeautifulSoup的对比

记得第一次用soup的时候,对于只用过Java sax解析xml的我来说,soup真的太方便了。今天简单测试了下crawl4ai,和soup相比

  • crawl4ai数据采集分析更方便
  • soup需要配合使用request进行网页抓取,BeautifulSoup负责html解析
  • html解析有点类似,都是通过CSS选择器,但crawl4ai通过定义schema,解析更方便
  • 数据解析方面,crawl4ai除了提供了markdown和简化版的html,还提供了通过集成OpenAI提取结构化数据的能力(尚未体验)
http://www.lryc.cn/news/451449.html

相关文章:

  • 仅有N卡独显的情况下安装ubuntu是遇到的黑屏,加载卡顿等问题
  • Vite:为什么选 Vite
  • 个人项目简单https服务配置
  • Rust 函数
  • 微信小程序中的 `<block>` 元素:高效渲染与结构清晰的利器
  • 选读算法导论5.2 指示器随机变量
  • 大数据-154 Apache Druid 架构与原理详解 基础架构、架构演进
  • centos9 nginx 版本
  • https访问报错:net::ERR_CERT_DATE_INVALLD
  • cat用来查看文件内容、合并文件,或者将文件内容输出到终端
  • 基于ssm大学生自主学习网站的设计与实现
  • C++基础补充(01)C++11基于范围的for循环
  • qt6 使用QPSQL
  • 【PostgreSQL】提高篇——公用表表达式(CTE)和窗口函数
  • 【min25筛】【CF2020F】Count Leaves
  • 【d57】【sql】1661. 每台机器的进程平均运行时间
  • ArcGIS共享数据的最佳方法(不丢可视化、标注等各类显示信息一样带)
  • 小程序this.getOpenerEventChannel()当前页面与navigateTo页面之间数据通信
  • 调用飞书接口导入供应商bug
  • 《深度学习》OpenCV 角点检测、特征提取SIFT 原理及案例解析
  • golang grpc初体验
  • 基于小程序+Vue + Spring Boot的进销存库存出库入库统计分析管理系统
  • 【数据结构与算法】时间复杂度和空间复杂度例题
  • 停止模式下USART为什么可以唤醒MCU?
  • Web安全 - 路径穿越(Path Traversal)
  • JSR303微服务校验
  • 56. QTreeWidget的基本使用
  • 领域偏移:协变量移位下的域自适应
  • 前端开发技术框架选型
  • /etc/init.d/mysql