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

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

Create an Array of Strings in Node from a URL

问题

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

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

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

基本上我想要:

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

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

  1. const getScript = (url) => {
  2. return new Promise((resolve, reject) => {
  3. const http = require('http'),
  4. https = require('https');
  5. let client = http;
  6. if (url.toString().indexOf("https") === 0) {
  7. client = https;
  8. }
  9. client.get(url, (resp) => {
  10. let data = '';
  11. // 收到数据块。
  12. resp.on('data', (chunk) => {
  13. data += chunk;
  14. });
  15. // 完整的响应已经接收到。打印结果。
  16. resp.on('end', () => {
  17. resolve(data);
  18. });
  19. }).on("error", (err) => {
  20. reject(err);
  21. });
  22. });
  23. };
  24. (async (url) => {
  25. console.log(await getScript(url));
  26. })('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:

  1. 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:

  1. const getScript = (url) => {
  2. return new Promise((resolve, reject) => {
  3. const http = require('http'),
  4. https = require('https');
  5. let client = http;
  6. if (url.toString().indexOf("https") === 0) {
  7. client = https;
  8. }
  9. client.get(url, (resp) => {
  10. let data = '';
  11. // A chunk of data has been recieved.
  12. resp.on('data', (chunk) => {
  13. data += chunk;
  14. });
  15. // The whole response has been received. Print out the result.
  16. resp.on('end', () => {
  17. resolve(data);
  18. });
  19. }).on("error", (err) => {
  20. reject(err);
  21. });
  22. });
  23. };
  24. (async (url) => {
  25. console.log(await getScript(url));
  26. })('https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt');

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

答案1

得分: 3

以下是翻译好的部分:

  1. 您可以像这样做
  2. <!-- begin snippet: js hide: false console: true babel: false -->
  3. <!-- language: lang-js -->
  4. const getScript = async (url) => {
  5. const response = await fetch(url);
  6. const data = await response.text();
  7. return data.split("\n");
  8. };
  9. (async () => {
  10. const httpProxies = await getScript(
  11. "https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt"
  12. );
  13. console.log(httpProxies);
  14. })();
  15. <!-- end snippet -->
  16. ---
  17. 如果您想知道如何在您的代码中执行此操作只需执行以下操作 `resolve(data.split('\n'));`
  18. ```js
  19. const getScript = (url) => {
  20. return new Promise((resolve, reject) => {
  21. const http = require('http'),
  22. https = require('https');
  23. let client = http;
  24. if (url.toString().indexOf('https') === 0) {
  25. client = https;
  26. }
  27. client
  28. .get(url, (resp) => {
  29. let data = '';
  30. // 收到数据块。
  31. resp.on('data', (chunk) => {
  32. data += chunk;
  33. });
  34. // 已接收到整个响应。打印结果。
  35. resp.on('end', () => {
  36. resolve(data.split('\n')); // 🔈 这里
  37. });
  38. })
  39. .on('error', (err) => {
  40. reject(err);
  41. });
  42. });
  43. };
  44. (async (url) => {
  45. console.log(await getScript(url));
  46. })(
  47. 'https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt'
  48. );
  1. <details>
  2. <summary>英文:</summary>
  3. You can do something like this:
  4. &lt;!-- begin snippet: js hide: false console: true babel: false --&gt;
  5. &lt;!-- language: lang-js --&gt;
  6. const getScript = async(url) =&gt; {
  7. const responce = await fetch(url);
  8. const data = await responce.text();
  9. return data.split(&quot;\n&quot;);
  10. };
  11. (async() =&gt; {
  12. const httpProxies = await getScript(
  13. &quot;https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt&quot;
  14. );
  15. console.log(httpProxies);
  16. })();
  17. &lt;!-- end snippet --&gt;
  18. ---
  19. And if you want to know how to do it with your code then just do this `resolve(data.split(&#39;\n&#39;));`
  20. ```js
  21. const getScript = (url) =&gt; {
  22. return new Promise((resolve, reject) =&gt; {
  23. const http = require(&#39;http&#39;),
  24. https = require(&#39;https&#39;);
  25. let client = http;
  26. if (url.toString().indexOf(&#39;https&#39;) === 0) {
  27. client = https;
  28. }
  29. client
  30. .get(url, (resp) =&gt; {
  31. let data = &#39;&#39;;
  32. // A chunk of data has been recieved.
  33. resp.on(&#39;data&#39;, (chunk) =&gt; {
  34. data += chunk;
  35. });
  36. // The whole response has been received. Print out the result.
  37. resp.on(&#39;end&#39;, () =&gt; {
  38. resolve(data.split(&#39;\n&#39;)); // &#128072; This
  39. });
  40. })
  41. .on(&#39;error&#39;, (err) =&gt; {
  42. reject(err);
  43. });
  44. });
  45. };
  46. (async (url) =&gt; {
  47. console.log(await getScript(url));
  48. })(
  49. &#39;https://raw.githubusercontent.com/monosans/proxy-list/main/proxies/http.txt&#39;
  50. );

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:

确定