使用 Node.js 在网页上实时查找比特币的价值
Using Node.js to find the value of Bitcoin on a webpage at real time
我正在尝试制作一个 .js 文件,它将不断更新比特币的价格(每五分钟左右)。我尝试了很多不同的网络抓取方式,但它们总是输出 null 或什么都不输出。这是我最新的代码,有什么想法吗?
var express = require('express');
var path = require('path');
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
var app = express();
var url = 'https://blockchain.info/charts/';
var port = 9945;
function BTC() {
request(url, function (err, res, body) {
var $ = cheerio.load(body);
var a = $(".market-price");
var b = a.text();
console.log(b);
})
setInterval(BTC, 300000)
}
BTC();
app.listen(port);
console.log('server is running on '+port);
它成功地说明了它 运行 所在的端口,这不是问题所在。这个例子(输出时)只是在每次函数发生时换行。
更新:
我更改了从 Wartoshika 获得的新代码,但它停止工作了,但我不确定为什么。这是:
function BTCPrice() {
request('https://blockchain.info/de/ticker', (error, response, body) => {
const data = JSON.parse(body);
var value = (parseInt(data.USD.buy, 10) + parseInt(data.USD.sell, 10)) / 2;
return value;
});
};
console.log(BTCPrice());
如果我直接从函数内部 console.log 得到它,它就可以工作,但是当我得到它时 console.log 函数的输出,它输出未定义的。有什么想法吗?
我宁愿使用 JSON api 而不是 HTML 解析器来获取当前的比特币值。使用 JSON api 你会得到一个可以被你的浏览器解析的直接结果集。
Url 看起来像 https://blockchain.info/de/ticker
工作脚本:
const request = require('request');
function BTC() {
// send a request to blockchain
request('https://blockchain.info/de/ticker', (error, response, body) => {
// parse the json answer and get the current bitcoin value
const data = JSON.parse(body);
value = (parseInt(data.THB.buy, 10) + parseInt(data.THB.sell, 10)) / 2;
console.log(value);
});
}
BTC();
使用值作为回调:
const request = require('request');
function BTC() {
return new Promise((resolve) => {
// send a request to blockchain
request('https://blockchain.info/de/ticker', (error, response, body) => {
// parse the json answer and get the current bitcoin value
const data = JSON.parse(body);
value = (parseInt(data.THB.buy, 10) + parseInt(data.THB.sell, 10)) / 2;
resolve(value);
});
});
}
BTC().then(val => console.log(val));
正如其他答案所述,您真的应该使用 API。您还应该考虑您想要请求的价格类型。如果您只想要一种汇总来自多个交易所的价格的指数价格,请使用 CoinGecko API 之类的东西。此外,如果您需要实时数据,则需要基于 websocket 的 API,而不是 REST API.
如果您需要特定交易所的价格,例如您正在为一个或多个交易所构建交易机器人,您将需要直接与每个交易所的 websoceket API 进行通信。为此,我会推荐类似 Coygo API 的东西,这是一个 node.js 包,可将您直接连接到每个交易所的实时数据馈送。您想要不添加中间人的东西,因为这会增加数据的延迟。
我正在尝试制作一个 .js 文件,它将不断更新比特币的价格(每五分钟左右)。我尝试了很多不同的网络抓取方式,但它们总是输出 null 或什么都不输出。这是我最新的代码,有什么想法吗?
var express = require('express');
var path = require('path');
var request = require('request');
var cheerio = require('cheerio');
var fs = require('fs');
var app = express();
var url = 'https://blockchain.info/charts/';
var port = 9945;
function BTC() {
request(url, function (err, res, body) {
var $ = cheerio.load(body);
var a = $(".market-price");
var b = a.text();
console.log(b);
})
setInterval(BTC, 300000)
}
BTC();
app.listen(port);
console.log('server is running on '+port);
它成功地说明了它 运行 所在的端口,这不是问题所在。这个例子(输出时)只是在每次函数发生时换行。
更新: 我更改了从 Wartoshika 获得的新代码,但它停止工作了,但我不确定为什么。这是:
function BTCPrice() {
request('https://blockchain.info/de/ticker', (error, response, body) => {
const data = JSON.parse(body);
var value = (parseInt(data.USD.buy, 10) + parseInt(data.USD.sell, 10)) / 2;
return value;
});
};
console.log(BTCPrice());
如果我直接从函数内部 console.log 得到它,它就可以工作,但是当我得到它时 console.log 函数的输出,它输出未定义的。有什么想法吗?
我宁愿使用 JSON api 而不是 HTML 解析器来获取当前的比特币值。使用 JSON api 你会得到一个可以被你的浏览器解析的直接结果集。
Url 看起来像 https://blockchain.info/de/ticker
工作脚本:
const request = require('request');
function BTC() {
// send a request to blockchain
request('https://blockchain.info/de/ticker', (error, response, body) => {
// parse the json answer and get the current bitcoin value
const data = JSON.parse(body);
value = (parseInt(data.THB.buy, 10) + parseInt(data.THB.sell, 10)) / 2;
console.log(value);
});
}
BTC();
使用值作为回调:
const request = require('request');
function BTC() {
return new Promise((resolve) => {
// send a request to blockchain
request('https://blockchain.info/de/ticker', (error, response, body) => {
// parse the json answer and get the current bitcoin value
const data = JSON.parse(body);
value = (parseInt(data.THB.buy, 10) + parseInt(data.THB.sell, 10)) / 2;
resolve(value);
});
});
}
BTC().then(val => console.log(val));
正如其他答案所述,您真的应该使用 API。您还应该考虑您想要请求的价格类型。如果您只想要一种汇总来自多个交易所的价格的指数价格,请使用 CoinGecko API 之类的东西。此外,如果您需要实时数据,则需要基于 websocket 的 API,而不是 REST API.
如果您需要特定交易所的价格,例如您正在为一个或多个交易所构建交易机器人,您将需要直接与每个交易所的 websoceket API 进行通信。为此,我会推荐类似 Coygo API 的东西,这是一个 node.js 包,可将您直接连接到每个交易所的实时数据馈送。您想要不添加中间人的东西,因为这会增加数据的延迟。