![图片[1]-这个爬虫太好用,学会了感觉要进去~~~-shaocun资源站](https://shaocun.xyz/wp-content/uploads/2025/01/frc-4fe7afd3bde79e032a3bcbc46c588dda.png)
使用场景:
-
网页抓取:如抓取新闻网站、社交媒体、商品信息等。
-
数据挖掘:从网页中提取结构化数据,用于市场分析、研究等。
![图片[2]-这个爬虫太好用,学会了感觉要进去~~~-shaocun资源站](https://shaocun.xyz/wp-content/uploads/2025/01/frc-b8faa332dfe8055f94db8aed1eec3eae.jpeg)
![图片[3]-这个爬虫太好用,学会了感觉要进去~~~-shaocun资源站](https://shaocun.xyz/wp-content/uploads/2025/01/frc-f46ffb4300ff848faa1f44d338f893c0.png)
安装测试
npx crawlee create my-crawler
cd my-crawler
npm start
- npm install crawlee playwright
-
新闻标题:通常使用 a 标签包裹,类名可能是 news-item 或其他动态类。 -
链接:嵌套在 a 标签的 href 属性里。
"https://news.sina.com.cn/article/123456" class="news-item"> 新浪新闻标题 /a>
import { PlaywrightCrawler } from 'crawlee';
const crawler = new PlaywrightCrawler({
// 页面处理逻辑
requestHandler: async ({ request, page, enqueueLinks, log }) => {
log.info(`Crawling: ${request.url}`);
// 提取新闻标题和链接
const articles = await page.$$eval('a', (links) =>
links
.filter((link) => link.textContent.trim() && link.href.includes('news.sina.com.cn'))
.map((link) => ({
title: link.textContent.trim(),
url: link.href,
}))
);
// 输出结果
articles.forEach((article) => {
console.log(`Title: ${article.title}`);
console.log(`Link: ${article.url}`);
console.log('---');
});
// 可选:自动发现并抓取更多链接
await enqueueLinks();
},
launchContext: {
launchOptions: {
headless: true, // 无头模式
},
},
});
(async () => {
// 添加抓取目标:新浪新闻首页
await crawler.addRequests(['https://news.sina.com.cn']);
// 开始运行爬虫
await crawler.run();
})();
![图片[4]-这个爬虫太好用,学会了感觉要进去~~~-shaocun资源站](https://shaocun.xyz/wp-content/uploads/2025/01/frc-6cc64ff8c285c6e5263572a403bb67a1.png)
![图片[5]-这个爬虫太好用,学会了感觉要进去~~~-shaocun资源站](https://shaocun.xyz/wp-content/uploads/2025/01/frc-fe9107f67e00109d5b37c184df1656ee.png)
pip install 'crawlee[all]'
playwright install
python -c 'import crawlee; print(crawlee.__version__)'
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容