如何使用 node.js 复制 wget 的功能?

编程入门 行业动态 更新时间:2024-10-17 23:26:05
本文介绍了如何使用 node.js 复制 wget 的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

是否可以在 node.js 应用程序中运行 wget ?我想要一个脚本来抓取一个站点,并下载一个特定的文件,但是文件链接的 href 经常更改.因此,我认为最简单的方法是找到链接的 href,然后对其执行 wget.

Is it possible to essentially run a wget from within a node.js app? I'd like to have a script that crawls a site, and downloads a specific file, but the href of the link that goes the file changes fairly often. So, I figured the easiest way to go about doing it would be to find the href of the link, then just perform a wget on it.

谢谢!

推荐答案

您可以使用 child_processes 运行外部命令:

You can run an external command using child_processes:

http://nodejs/docs/latest/api/child_process.html#child_process_child_process_exec_command_options_callback

var util = require('util'),
    exec = require('child_process').exec,
    child,
    url = 'url to file';

child = exec('wget ' + url,
  function (error, stdout, stderr) {
    console.log('stdout: ' + stdout);
    console.log('stderr: ' + stderr);
    if (error !== null) {
      console.log('exec error: ' + error);
    }
});

这篇关于如何使用 node.js 复制 wget 的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-26 04:04:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1128623.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   功能   js   node   wget

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!