node.js - 如何获取 URL 的 HTML
问题描述
我想将 URL 的 HTML 内容检索到一个字符串中,以便我可以使用str.search('XXX')
,如果 XXX 在那里,我将执行一个操作。
if (fetch("https://steamcommunity.com/id/" + message.channel.lastMessage)
.then(res => res.text())
.then(body => console.log(body))
// I NEED TO TRANSFORM THE HTML INTO STRING AND USE str.search('The specified profile could not be found.'))
{
message.channel.lastMessage.edit('URL "' + message.channel.lastMessage + '" is not taken.')
}
else {return}
这就是我目前所拥有的,欢迎任何其他实现相同目标的选择。
编辑2:
我按照某人的建议运行了将 res.text() 替换为 console.log(res) 的代码,这是输出:
$ node index.js
Response {
size: 0,
timeout: 0,
[Symbol(Body internals)]: {
body: Gunzip {
_writeState: [Uint32Array],
_readableState: [ReadableState],
readable: true,
_events: [Object],
_eventsCount: 7,
_maxListeners: undefined,
_writableState: [WritableState],
writable: true,
allowHalfOpen: true,
_transformState: [Object],
_hadError: false,
bytesWritten: 0,
_handle: [Zlib],
_outBuffer: <Buffer 88 e2 7c 69 2e 7f 00 00 e0 35 f4 02 00 00 00 00 b0 d8 eb 02 00 00 00 00 b0 d8 eb 02 00 00 00 00 67 22 2c 20 22 73 68 61 72 64 73 22 3a 20 31 2c 20 22 ... 16334 more bytes>,
_outOffset: 0,
_chunkSize: 16384,
_defaultFlushFlag: 2,
_finishFlushFlag: 2,
_nextFlush: -1,
_defaultFullFlushFlag: 3,
_info: undefined,
_level: -1,
_strategy: 0
},
disturbed: false,
error: null
},
[Symbol(Response internals)]: {
url: 'https://stackoverflow.com/questions/51120073/how-to-send-a-message-to-a-specific-channel',
status: 200,
statusText: 'OK',
headers: Headers { [Symbol(map)]: [Object] },
counter: 0
}
}
undefined
这不是整个 HTML 正文,但它告诉我页面的状态,所以如果我能找到一种在状态为 404 时执行操作的方法,那将是我试图实现的同一件事。
解决方案
推荐阅读
- c# - 将在 foreach 循环中生成的 Azure Cosmos Db SQL-API 语句转换为参数化语句
- google-classroom - 403 当前用户在为 Google 课堂 api (.Net) 创建课程时无法创建课程 [禁止]
- php - 一个用户奇怪地出现了两个链接 - Laravel 模型
- github-actions - 不同prettier的检查结果
- sql - 如何从 Azure 数据工厂中的 JSON 获取数组?
- angular - 如何在二级项目中调用Angular Element中定义的模块?
- angular - ChromeHeadless 未在 docker 映像中开始使用
- swiftui - 在 SwiftUI 中对 UI 进行更新时关闭工具栏菜单
- google-cloud-platform - 目录的 GCP 推荐数据格式
- google-sheets - 在数据透视表中显示逗号分隔的值列表