在 Nodejs 中将 HTML 字符串解析为 JS [英] Parse HTML string to JS in Nodejs

查看:46
本文介绍了在 Nodejs 中将 HTML 字符串解析为 JS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有更好的方法来捕获输入标签内的值,而不是在 JS 中使用正则表达式.

I was wondering if there was a better way to capture a value inside an input tag rather than using regex in JS.

"<html><head></head><body onload=\"document.form1.submit()\"><form name=\"form1\" method=\"post\" action=\"\" ><input name=\"Token\" type=\"hidden\" value=\"\"><input name=\"ID\" type=\"hidden\" value=\"12120012732dafd4\"></form></body></html>"

理想情况下,我只想捕获 ID 值 12120012732dafd4

Ideally I would like to capture just the ID value 12120012732dafd4

推荐答案

您可以使用 cheerio:

h = "[your HTML]"
const $ = cheerio.load(h)
console.log("Value:", $("form input[name='ID']").attr("value"))

演示:https://runkit.com/adelriosantiago/get-attr-from-html-in-node

或者,您可以使用 jsdomhtmlparser.

Alternatively you can use jsdom or htmlparser.

这篇关于在 Nodejs 中将 HTML 字符串解析为 JS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆