在Node中从URL创建一个字符串数组

huangapple go评论88阅读模式
英文:

Create an Array of Strings in Node from a URL

问题

我想从以下URL获取文本内容:

https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt

然后创建一个名为httpProxies的数组,其中URL中的每一行代表数组中的一个值。

基本上我想要:

console.log(httpProxies); // 返回 [ '78.110.195.242:7080', '144.86.187.56:8888', '64.225.4.85:9999', '34.146.64.228:3128', ...]

我已经开始使用这段代码:

const getScript = (url) => {
  return new Promise((resolve, reject) => {
    const http = require('http'),
          https = require('https');

    let client = http;

    if (url.toString().indexOf("https") === 0) {
        client = https;
    }

    client.get(url, (resp) => {
        let data = '';

        // 收到数据块。
        resp.on('data', (chunk) => {
            data += chunk;
        });

        // 完整的响应已经接收到。打印结果。
        resp.on('end', () => {
            resolve(data);
        });

    }).on("error", (err) => {
        reject(err);
    });
});
};

(async (url) => {
    console.log(await getScript(url));
})('https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt');

但我不确定如何创建数组。有什么想法吗?

英文:

I want to take the txt contents of this URL:

https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt

And create an array named httpProxies where each line from the URL represents a value in the array.

Basically I want:

console.log(httpProxies); //to return [ '78.110.195.242:7080', '144.86.187.56:8888', '64.225.4.85:9999', '34.146.64.228:3128', ...]

I began using this code:

const getScript = (url) => {
  return new Promise((resolve, reject) => {
    const http      = require('http'),
          https     = require('https');

    let client = http;

    if (url.toString().indexOf("https") === 0) {
        client = https;
    }

    client.get(url, (resp) => {
        let data = '';

        // A chunk of data has been recieved.
        resp.on('data', (chunk) => {
            data += chunk;
        });

        // The whole response has been received. Print out the result.
        resp.on('end', () => {
            resolve(data);
        });

    }).on("error", (err) => {
        reject(err);
    });
});
};

(async (url) => {
    console.log(await getScript(url));
})('https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt');

But I am unsure how to create the array. Any ideas?

答案1

得分: 3

以下是翻译好的部分:

您可以像这样做

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    const getScript = async (url) => {
      const response = await fetch(url);
      const data = await response.text();
      return data.split("\n");
    };

    (async () => {
      const httpProxies = await getScript(
        "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt"
      );
      console.log(httpProxies);
    })();

<!-- end snippet -->

---

如果您想知道如何在您的代码中执行此操作只需执行以下操作 `resolve(data.split('\n'));`

```js
const getScript = (url) => {
  return new Promise((resolve, reject) => {
    const http = require('http'),
      https = require('https');

    let client = http;

    if (url.toString().indexOf('https') === 0) {
      client = https;
    }

    client
      .get(url, (resp) => {
        let data = '';

        // 收到数据块。
        resp.on('data', (chunk) => {
          data += chunk;
        });

        // 已接收到整个响应。打印结果。
        resp.on('end', () => {
          resolve(data.split('\n')); // 🔈 这里
        });
      })
      .on('error', (err) => {
        reject(err);
      });
  });
};

(async (url) => {
  console.log(await getScript(url));
})(
  'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt'
);

<details>
<summary>英文:</summary>

You can do something like this:

&lt;!-- begin snippet: js hide: false console: true babel: false --&gt;

&lt;!-- language: lang-js --&gt;

    const getScript = async(url) =&gt; {
      const responce = await fetch(url);
      const data = await responce.text();
      return data.split(&quot;\n&quot;);
    };

    (async() =&gt; {
      const httpProxies = await getScript(
        &quot;https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt&quot;
      );
      console.log(httpProxies);
    })();

&lt;!-- end snippet --&gt;


---

And if you want to know how to do it with your code then just do this `resolve(data.split(&#39;\n&#39;));`

```js
const getScript = (url) =&gt; {
  return new Promise((resolve, reject) =&gt; {
    const http = require(&#39;http&#39;),
      https = require(&#39;https&#39;);

    let client = http;

    if (url.toString().indexOf(&#39;https&#39;) === 0) {
      client = https;
    }

    client
      .get(url, (resp) =&gt; {
        let data = &#39;&#39;;

        // A chunk of data has been recieved.
        resp.on(&#39;data&#39;, (chunk) =&gt; {
          data += chunk;
        });

        // The whole response has been received. Print out the result.
        resp.on(&#39;end&#39;, () =&gt; {
          resolve(data.split(&#39;\n&#39;)); // &#128072; This
        });
      })
      .on(&#39;error&#39;, (err) =&gt; {
        reject(err);
      });
  });
};

(async (url) =&gt; {
  console.log(await getScript(url));
})(
  &#39;https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt&#39;
);

huangapple
  • 本文由 发表于 2023年7月11日 03:28:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76656749.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定