简体   繁体   English

我可以在手动创建的文档模型上应用 jQuery 吗?

[英]Can I apply jQuery on manually created document model?

I am trying to scrape some webpage, doing some searching I get to know about fetch API.我正在尝试抓取一些网页,进行一些搜索以了解 fetch API。

I have fetched a webpage using fetch() API from an URL , then I parsed the page into a DOM object, Now I have whole webpage in a DOM object.我使用 fetch() API 从 URL 获取了一个网页,然后我将页面解析为 DOM 对象,现在我在 DOM 对象中有整个网页。 Can I apply jQuery functions on that?我可以在上面应用 jQuery 函数吗?

my code我的代码

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 is not mandatory for me. .ready 对我来说不是强制性的。 I just need to extract some data from doc object.我只需要从 doc 对象中提取一些数据。 if there is any better way to fetch data from webpage please let me know, it would be very helpful for me.如果有更好的方法从网页中获取数据,请告诉我,这对我很有帮助。 Thank you so much.非常感谢。

You do not need and jQuery here:你不需要和 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM