Extracting Metadata from Website

前端 未结 2 1393
青春惊慌失措
青春惊慌失措 2021-01-27 00:12

I was wondering if there\'s a way in javascript that allows me to process the html source code that allows me to take out specific tags that I want?

Sorry if it sounds

相关标签:
2条回答
  • 2021-01-27 00:26

    If you have the HTML in a string, then you can use:

    var str = '<html></html>'; // your html text goes here
    var div = document.createElement('div');
    div.innerHTML = str;
    var dom = div.firstChild; // dom is the object you want,
                              // you can manipulate it using standard dom methods
    

    Alternately, use jQuery. jQuery is a library to help you manipulate and access HTML elements more easily. First, add this to the head of your document:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
    

    This is a reference to the jQuery library. Then, do:

    var foo = $("<html>Your html here</html>");
    

    Or, if your html is in a variable (e.g. str), you can do:

    var foo = $(str);
    

    Then, you can manipulate and parse foo in a number of ways. For example, to remove all paragraph elements, you would use

    foo.remove('p');
    

    Or, to remove the paragraph element with id="bar", use:

    foo.remove('p.bar');
    

    Once you are done your modifications, you can get the new html text using:

    foo.html();
    

    Why is your html in a string? Is it not the html of the current page?

    0 讨论(0)
  • 2021-01-27 00:29

    Use DOM it can pull data from webpages if you know the structure.

    0 讨论(0)
提交回复
热议问题