javascript - 不能使用来自 URL 的代理
问题描述
我可以毫无问题地使用代理
var proxies = fs.readFileSync('proxies.txt', 'utf-8').replace(/\r/gi, '').split('\n');
但是当我这样做时
(function scrapeProxies() {
request.get('https://mezy.wtf/proxies.txt', (err, res, body) => {
proxies = body.split('\n');
setTimeout(() => scrapeProxies(), 2 * 60 * 1000);
});
无论我做什么,它似乎都不起作用,给出错误
“TypeError:无法读取 null 的属性‘长度’”
如果有人可以帮助我,那就太好了,因为我对此很陌生,只是在学习!将不胜感激,谢谢。
这是我的代码,因为太长了,我无法在此处上传,无法全部粘贴:
解决方案
根据您的代码,以下应该可以工作
const scrapeProxies = () => {
request.get('https://mezy.wtf/proxies.txt', (err, res, body) => {
if (err) throw err;
proxies = body.replace(/\r/gi, '').split('\n');
setTimeout(() => scrapeProxies(), 2 * 60 * 1000);
})
};
scrapeProxies();
您的代码存在以下问题:
1st - 您需要检查完成的请求中是否有任何错误
if (err) throw err;
这意味着如果发现错误,它将抛出异常。
2nd - 在你用 '\n' 分割后,数组中的每个字符串仍然有 '\r',这就是为什么你需要像第一个示例中所做的那样将其替换为空。
body.replace(/\r/gi, '')
或者你可以直接用 '\r\n' 分割。
body.split('\r\n');
推荐阅读
- swift - 无法在集合视图上获取 indexPath 和选择
- java - 如何在 JSP 页面中显示列表结果?
- html - 如何在 Apple tv 应用程序中打开 HTML 文件?
- swift - 滚动表格视图时删除的文本字段值?
- laravel - Laravel 无法附加多对多表
- html - 鼠标在按钮中间时菜单不可点击
- c++ - 当 std::bind-ing std::function 与 std::future 时调用运算符不匹配
- visual-studio - 为什么VS 2019没有加载MSBuild.Sdk.Extras项目sdk类型?
- android - Android 夜间模式跟随系统无法正常工作
- javascript - 按钮单击时反应状态不改变