我正在尝试抓取一些网页,做一些搜索,我得到了关于fetch API的信息。
我已经使用fetch() API从URL获取了一个网页,然后我将该页面解析为一个DOM对象,现在我将整个网页放在一个DOM对象中。我可以在上面应用jQuery函数吗?
我的代码
async function getProductData(url)
{
try {
const resp = await fetch(url);
var respText = await resp.text();
var parser = new DOMParser();
var doc = parser.parseFromString(respText, 'text/html')
// I am trying to do something like that. is it possible to do so ?
$(doc).ready( function(){
console.log( $( this) .find( $("#productTitle") ).text() );
});
}
catch (error) {
console.log(error);
}
}
.ready对我来说不是强制性的。我只需要从doc对象中提取一些数据。如果有更好的方法从网页上获取数据请让我知道,这将是非常有帮助的我。非常感谢。
发布于 2020-10-18 09:36:05
这里不需要和jQuery:
const resp = await fetch(url);
const respText = await resp.text();
const parser = new DOMParser();
const doc = parser.parseFromString(respText, 'text/html');
console.log(doc.querySelector('#productTitle').innerText);
https://stackoverflow.com/questions/64411735
复制相似问题