x-crawl
x-crawl 是一个灵活的 Node.js 多功能爬虫库,支持异步/同步爬取、动态/静态页面爬取、接口数据获取、文件下载等功能,具有失败重试、轮换代理、设备指纹等特性,帮助用户快速、安全、稳定地进行网络爬取。
• Copy the embed code to showcase this product on your website
• Share on X to spread the word about this amazing tool
x-crawl 是一个灵活的 Node.js 多功能爬虫库,支持异步/同步爬取、动态/静态页面爬取、接口数据获取、文件下载等功能,具有失败重试、轮换代理、设备指纹等特性,帮助用户快速、安全、稳定地进行网络爬取。
• Copy the embed code to showcase this product on your website
• Share on X to spread the word about this amazing tool
js// 1.导入模块 ES/CJSimport xCrawl from 'x-crawl'// 2.创建一个爬虫实例const myXCrawl = xCrawl({ maxRetry: 3, intervalTime: { max: 2000, min: 1000 } })// 3.设置爬取任务// 调用 startPolling API 开始轮询功能,每隔一天会调用回调函数myXCrawl.startPolling({ d: 1 }, async (count, stopPolling) => { // 调用 crawlPage API 来爬取页面 const pageResults = await myXCrawl.crawlPage({ targets: [ 'https://www.airbnb.cn/s/*/experiences', 'https://www.airbnb.cn/s/plus_homes' ], viewport: { width: 1920, height: 1080 } }) // 通过遍历爬取页面结果获取图片 URL const imgUrls = [] for (const item of pageResults) { const { id } = item const { page } = item.data const elSelector = id === 1 ? '.i9cqrtb' : '.c4mnd7m' // 等待页面元素出现 await page.waitForSelector(elSelector) // 获取页面图片的 URL const urls = await page.$$eval(`${elSelector} picture img`, (imgEls) => imgEls.map((item) => item.src) ) imgUrls.push(...urls.slice(0, 6)) // 关闭页面 page.close() } // 调用 crawlFile API 爬取图片 await myXCrawl.crawlFile({ targets: imgUrls, storeDirs: './upload' })})
运行效果:<div align="center"> <img src="https://raw.githubusercontent.com/coder-hxl/x-crawl/main/assets/run-example.gif" /></div>注意: 请勿随意爬取,爬取前可查看 robots.txt 协议。网站的类名可能会有变更,这里只是为了演示如何使用 x-crawl 。## 更多更多内容可查看:https://github.com/coder-hxl/x-crawl