前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >记入一次爬虫之旅

记入一次爬虫之旅

作者头像
用户4792657
发布2022-07-06 14:12:39
6160
发布2022-07-06 14:12:39
举报
文章被收录于专栏:山海亦有归期

Plain text or HTML

代码语言:javascript
复制
import fetch from 'node-fetch';

const response = await fetch('https://github.com/');
const body = await response.text();

console.log(body);
复制代码

Streams

The "Node.js way" is to use streams when possible. You can pipe res.body to another stream. This example uses stream.pipeline to attach stream error handlers and wait for the download to complete.

代码语言:javascript
复制
import {createWriteStream} from 'node:fs';
import {pipeline} from 'node:stream';
import {promisify} from 'node:util'
import fetch from 'node-fetch';

const streamPipeline = promisify(pipeline);

const response = await fetch('https://github.githubassets.com/images/modules/logos_page/Octocat.png');

if (!response.ok) throw new Error(`unexpected response ${response.statusText}`);

await streamPipeline(response.body, createWriteStream('./octocat.png'));
复制代码
代码语言:javascript
复制
import fetch from 'node-fetch';
import cheerio from 'cheerio';
import {createWriteStream} from 'node:fs';
import {pipeline} from 'node:stream';
import {promisify} from 'node:util'

const response = await fetch('https://mmzztt.com/beauty/page/10');
const body = await response.text();
const $ = cheerio.load(body);
const downloadImg = async (url, index) => {
  const streamPipeline = promisify(pipeline);
  const date = new Date().getFullYear()+'-'+ (new Date().getMonth())+'-'+(new 
 Date().getDate()+1)
  const response = await fetch(url);

  if (!response.ok) throw new Error(`unexpected response ${response.statusText}`);

  await streamPipeline(response.body, createWriteStream(`./img/${date}-${index}.png`));
  console.log(`已爬取第${index}张`)
}
$('.uk-article .uk-inline .u-thumb-f img').each(function (i) {
  downloadImg($(this).attr('data-src'), i)
})

复制代码
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-02-16,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Plain text or HTML
  • Streams
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档