我想实现的目标 在React内部,我想使用fetch来获取HTML页面,并从响应中得到我想要从div中提取一些数据的类,该类名为“ myDiv”
我要提取的数据为HTML格式。目前,我有这段代码可以提取数据并以文本格式解析响应。
fetch('/my/url')
.then(response => response && response.text())
.then(text => console.log('Do something that extract content in class "myDiv"));
答案 0 :(得分:2)
您可以执行类似的操作,只是普通的Vanila JS。
unsigned char bytes[] = { 10, 55, 255 };
struct pair_hash
{
std::size_t operator() (const std::pair<int, int> &p)const
{
return std::hash<int>()(p.first) ^ std::hash<int>()(p.first); // Possibly want a better hash
}
};
std::unordered_map<std::pair<int, int>, long long, pair_hash> map;
std::unordered_map<long long, long long> map;
答案 1 :(得分:1)
如上所述,您可以使用DOMParser
const parser = new DOMParser(),
dom = parser.parseFromString(text, "text/html");
然后使用惯常方法访问dom
let myDivContent = dom.querySelector('.myDiv').innerHTML;
答案 2 :(得分:0)
尝试DOMParser,它将字符串中的XML或HTML源代码解析为DOM文档。
例如:
const parser = new DOMParser();
const doc = parser.parseFromString(stringContainingHTMLSource, "text/html");
// returns a HTMLDocument, which also is a Document.
const myDiv = doc.querySelector('.myDiv');
console.log(myDiv.innerText);
答案 3 :(得分:0)
字符串格式的HTML元素
var txt = JSON.stringify("<div class='c1'>Welcome</div>")
JSON.parse()将字符串转换为html元素,即[object Object]
document.getElementById("demo").innerHTML = JSON.parse(txt)