为什么我的回复看起来像这样?节点获取
Why does my response look like this? Node-Fetch
我是 Node JS 的新手,我正在尝试从此 URL 获取数据:https://www.tiktok.com/node/share/user/@test/
您无需登录任何内容即可查看数据,只需单击 link 即可看到我要访问的数据。但是,当我尝试使用 node-fetch 检索此数据以便我可以使用它时,我得到了一个奇怪的响应(或者,更好的说法是,我不知道这意味着什么哈哈)
Response {
size: 0,
[Symbol(Body internals)]: {
body: BrotliDecompress {
_writeState: [Uint32Array],
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
bytesWritten: 0,
_handle: [BrotliDecoder],
_outBuffer: <Buffer 80 71 b8 8d 94 7f 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 50 29 84 02 00 00 00 00 90 23 84 02 00 00 00 00 00 00 ... 16334 more bytes>,
_outOffset: 0,
_chunkSize: 16384,
_defaultFlushFlag: 0,
_finishFlushFlag: 2,
_defaultFullFlushFlag: 1,
_info: undefined,
_maxOutputLength: 4294967296,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null,
[Symbol(kError)]: null
},
stream: BrotliDecompress {
_writeState: [Uint32Array],
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
bytesWritten: 0,
_handle: [BrotliDecoder],
_outBuffer: <Buffer 80 71 b8 8d 94 7f 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 50 29 84 02 00 00 00 00 90 23 84 02 00 00 00 00 00 00 ... 16334 more bytes>,
_outOffset: 0,
_chunkSize: 16384,
_defaultFlushFlag: 0,
_finishFlushFlag: 2,
_defaultFullFlushFlag: 1,
_info: undefined,
_maxOutputLength: 4294967296,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null,
[Symbol(kError)]: null
},
boundary: null,
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
type: 'default',
url: 'https://www.tiktok.com/node/share/user/@test',
status: 200,
statusText: 'OK',
headers: {
'cache-control': 'max-age=0, no-cache, no-store',
connection: 'close',
'content-encoding': 'br',
'content-length': '991',
'content-type': 'application/json; charset=utf-8',
date: 'Wed, 30 Mar 2022 20:20:29 GMT',
expires: 'Wed, 30 Mar 2022 20:20:29 GMT',
pragma: 'no-cache',
'referrer-policy': 'strict-origin-when-cross-origin',
server: 'nginx',
'server-timing': [Array],
'set-cookie': [Array],
'strict-transport-security': 'max-age=31536000; includeSubDomains',
'x-akamai-request-id': '3784d080.1aa6322a',
'x-cache': 'TCP_MISS from a23-48-94-141.deploy.akamaitechnologies.com (AkamaiGHost/10.7.3.1-40349883) (-)',
'x-cache-remote': 'TCP_MISS from a23-15-9-28.deploy.akamaitechnologies.com (AkamaiGHost/10.7.3.1-40349883) (-)',
'x-content-type-options': 'nosniff',
'x-download-options': 'noopen',
'x-frame-options': 'SAMEORIGIN',
'x-ms-token': 'jtKW5E_FJyaGwbk6hof4wjMp-LxmnCeL3iIVLSNu5BjJXmX16YSt-nHq63kbAL0QIiIf43Kmi9EjXgk2Qvd2K2aaz7p91th3qsRD',
'x-origin-response-time': '189,23.15.9.28',
'x-parent-response-time': '209,23.48.94.141',
'x-tt-logid': '202203302020280101130060380B3C7E54',
'x-tt-trace-host': '013d901111266bc0a1c59117d43662ef4d189a61edb84152e27f18c97ac8160a34e528b75987e12883be0879504e89313478ad3ad6007087782ae6295db5253d379511105161d49f40cc7c706ee0e3592a4ccaeeb591ac76187f951abae0ae5c7b',
'x-tt-trace-tag': 'id=16;cdn-cache=miss;type=dyn',
'x-ua-compatible': 'IE=edge,chrome=1',
'x-xss-protection': '1; mode=block'
},
counter: 0,
highWaterMark: 16384
}
}
这是我使用的代码。
const fetch = require('node-fetch')
fetch("https://www.tiktok.com/node/share/user/@test/")
.then(res => {
console.log(res);
})
我看了整篇文章,不太明白这意味着什么。抱歉问了,但是是的
您正在查看的是 Response
对象,它表示 整个 HTTP 响应。如果您只对 JSON 数据感兴趣,您可以使用 Response.json()
方法提取它:
const fetch = require("node-fetch");
// you'll get an "invalid json response body" error without this
const headers = { "User-Agent": "node-fetch" };
const response = fetch("https://www.tiktok.com/node/share/user/@test", {
headers,
})
// first, extract JSON data from `Response` object
.then((res) => res.json())
// then print JSON data that was parsed
.then((data) => console.log(data));
我是 Node JS 的新手,我正在尝试从此 URL 获取数据:https://www.tiktok.com/node/share/user/@test/
您无需登录任何内容即可查看数据,只需单击 link 即可看到我要访问的数据。但是,当我尝试使用 node-fetch 检索此数据以便我可以使用它时,我得到了一个奇怪的响应(或者,更好的说法是,我不知道这意味着什么哈哈)
Response {
size: 0,
[Symbol(Body internals)]: {
body: BrotliDecompress {
_writeState: [Uint32Array],
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
bytesWritten: 0,
_handle: [BrotliDecoder],
_outBuffer: <Buffer 80 71 b8 8d 94 7f 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 50 29 84 02 00 00 00 00 90 23 84 02 00 00 00 00 00 00 ... 16334 more bytes>,
_outOffset: 0,
_chunkSize: 16384,
_defaultFlushFlag: 0,
_finishFlushFlag: 2,
_defaultFullFlushFlag: 1,
_info: undefined,
_maxOutputLength: 4294967296,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null,
[Symbol(kError)]: null
},
stream: BrotliDecompress {
_writeState: [Uint32Array],
_readableState: [ReadableState],
_events: [Object: null prototype],
_eventsCount: 6,
_maxListeners: undefined,
_writableState: [WritableState],
allowHalfOpen: true,
bytesWritten: 0,
_handle: [BrotliDecoder],
_outBuffer: <Buffer 80 71 b8 8d 94 7f 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 70 a9 71 02 00 00 00 00 50 29 84 02 00 00 00 00 90 23 84 02 00 00 00 00 00 00 ... 16334 more bytes>,
_outOffset: 0,
_chunkSize: 16384,
_defaultFlushFlag: 0,
_finishFlushFlag: 2,
_defaultFullFlushFlag: 1,
_info: undefined,
_maxOutputLength: 4294967296,
[Symbol(kCapture)]: false,
[Symbol(kCallback)]: null,
[Symbol(kError)]: null
},
boundary: null,
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
type: 'default',
url: 'https://www.tiktok.com/node/share/user/@test',
status: 200,
statusText: 'OK',
headers: {
'cache-control': 'max-age=0, no-cache, no-store',
connection: 'close',
'content-encoding': 'br',
'content-length': '991',
'content-type': 'application/json; charset=utf-8',
date: 'Wed, 30 Mar 2022 20:20:29 GMT',
expires: 'Wed, 30 Mar 2022 20:20:29 GMT',
pragma: 'no-cache',
'referrer-policy': 'strict-origin-when-cross-origin',
server: 'nginx',
'server-timing': [Array],
'set-cookie': [Array],
'strict-transport-security': 'max-age=31536000; includeSubDomains',
'x-akamai-request-id': '3784d080.1aa6322a',
'x-cache': 'TCP_MISS from a23-48-94-141.deploy.akamaitechnologies.com (AkamaiGHost/10.7.3.1-40349883) (-)',
'x-cache-remote': 'TCP_MISS from a23-15-9-28.deploy.akamaitechnologies.com (AkamaiGHost/10.7.3.1-40349883) (-)',
'x-content-type-options': 'nosniff',
'x-download-options': 'noopen',
'x-frame-options': 'SAMEORIGIN',
'x-ms-token': 'jtKW5E_FJyaGwbk6hof4wjMp-LxmnCeL3iIVLSNu5BjJXmX16YSt-nHq63kbAL0QIiIf43Kmi9EjXgk2Qvd2K2aaz7p91th3qsRD',
'x-origin-response-time': '189,23.15.9.28',
'x-parent-response-time': '209,23.48.94.141',
'x-tt-logid': '202203302020280101130060380B3C7E54',
'x-tt-trace-host': '013d901111266bc0a1c59117d43662ef4d189a61edb84152e27f18c97ac8160a34e528b75987e12883be0879504e89313478ad3ad6007087782ae6295db5253d379511105161d49f40cc7c706ee0e3592a4ccaeeb591ac76187f951abae0ae5c7b',
'x-tt-trace-tag': 'id=16;cdn-cache=miss;type=dyn',
'x-ua-compatible': 'IE=edge,chrome=1',
'x-xss-protection': '1; mode=block'
},
counter: 0,
highWaterMark: 16384
}
}
这是我使用的代码。
const fetch = require('node-fetch')
fetch("https://www.tiktok.com/node/share/user/@test/")
.then(res => {
console.log(res);
})
我看了整篇文章,不太明白这意味着什么。抱歉问了,但是是的
您正在查看的是 Response
对象,它表示 整个 HTTP 响应。如果您只对 JSON 数据感兴趣,您可以使用 Response.json()
方法提取它:
const fetch = require("node-fetch");
// you'll get an "invalid json response body" error without this
const headers = { "User-Agent": "node-fetch" };
const response = fetch("https://www.tiktok.com/node/share/user/@test", {
headers,
})
// first, extract JSON data from `Response` object
.then((res) => res.json())
// then print JSON data that was parsed
.then((data) => console.log(data));