我有一个返回整个 HTML 页面的 get 请求,我想从该页面中获取许多“td”标签之一下的特定信息。标签很简单,没有任何类或 ID。
axios.get("https://example.com")
.then(res =>{
console.log(res.data.body) // returns an empty array
console.log(res.data) // returns the whole html page
//Here I tried somehow to pass the html page to the document
//so i could try use jquery or javascript to find the tags
//This does not work
document = res.data
var test = document.getElementsByTagName("td")
//Here I wanted to loop through the document to find the information
for(let index = 0; index < res.data.length; index++)
{
console.log(res.data[index])
//this outputs each character of the html page
if(res.data[index].innerHTML == "label")
{
console.log(res.data[index+1])
}
}
})
我想找到一个带有特定innerHTML 的TD 标签。这就像一个标签,它总是一样的。然后我会使用类似 res.data[index+1] 的东西来获取下一个标签并拉出innerHTML。
有没有办法做到这一点?
如何使用 javascript 在 get 请求的响应中搜索 html 标签?
该响应来自第三方 API。
您可以创建一个元素并将返回的 html 填充到其中,以便您拥有一个可以运行查询的变量/元素。
像这样的东西
axios.get("https://example.com")
.then(res => {
const doc = document.createElement("div");
doc.innerHTML = res.data;
console.log( doc.querySelectorAll("td") );
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句