In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "what are the common HTML parsing libraries under JS", which are easy to understand and well-organized, hoping to help you solve your doubts, let the editor lead you to study and learn "what are the common HTML parsing libraries under JS"?
DOMParser
The DOM manipulation capabilities of JavaScript and jQuery are well suited for parsing simple HTML fragments. In actual programming, if you want to programmatically parse DOM's full HTML or XML, you need a better solution: DOMParser, which is supported by all modern digital browsers.
By using DOMParser, you can easily parse HTML documents. However, parsing generally needs to be achieved by deceiving the browser, for example, by adding new elements to the current document.
The use of DOMParser is simple and straightforward:
Let domParser = new DOMParser (); let doc = domParser.parseFromString (stringContainingXMLSource, "application/xml"); domParser = new DOMParser (); doc = domParser.parseFromString (stringContainingSVGSource, "image/svg+xml"); domParser = new DOMParser (); doc = domParser.parseFromString (stringContainingHTMLSource, "text/html")
Cheerio
Fast, flexible and exquisite implementation of the core jQuery designed for servers.
Cheerio looks like jQuery, but does not support browsers. Cheerio can parse HTML and make it easy to operate, but it doesn't interpret HTML as it does in a browser, parsing something different from the browser, and the result of parsing is not sent directly to the user.
Cheerio implements a subset of jQuery, removes everything in jQuery that is inconsistent with DOM or is used to fill browsers, and recreates jQuery's most wonderful API
Due to the use of the extremely simple and standard DOM model, Cheerio is extremely efficient in document conversion, operation, and rendering.
JavaScript developers should be familiar with the syntax and usage of Cheerio:
Var chro = require ('cheerio'), $= chio.load (' Hello Worldwide'); $('h2.title`). Text (' Hello roomchongfang'); $('H2'). Attr ('id',' welcome'); $.html (); result: Hello Chongchong!
Jsdom
Jsdom is a pure JavaScript implementation of many Web standards (especially WHATWG DOM and HTML standards) and can be used in conjunction with Node.js. The goal of the jsdom project is to simulate a subset of Web browsers to test and crawl actual Web applications.
Jsdom is not just a HTML parser, it can also be used as a browser. In the context of parsing, if the necessary tags are omitted from the data to be parsed, it automatically adds the necessary tags. For example, if there is no html tag, it will add it implicitly like a browser.
You can also choose to specify some properties, such as the document, the referral source URL, or the URL of the user agent. This URL is especially useful if you need to resolve links that contain local URL.
Since it actually has nothing to do with parsing, it only mentions that jsdom has a (virtual) console, support for cookie, and so on. In short, you need to simulate a browser environment
It can also handle external resources. Jsdom can be used to load and execute JS scripts if required.
Const jsdom = require ("jsdom"); const {JSDOM} = jsdom;const dom = new JSDOM ('
Hello, Chongchong!
); console.log (dom.window.document.querySelector ("p") .textContent)
Results:
"Hello, Chongchong!"
Parse5
Parse5 provides almost everything you need to deal with HTML. The Parse5 library, which aims to build other tools, but can also implement HTML parsing to accomplish simple tasks. Parse5 is easy to use, but does not provide browsers with methods to manipulate DOM (such as getElementById).
Parse5 has spawned a series of impressive projects that adopted it: jsdom,Angular2 and Polymer. If the requirements are a reliable basis for advanced operation or parsing of HTML, then obviously this is a good choice.
Const parse5 = require ('parse5'); const document = parse5.parse (' Hello readers'); console.log (document.childNodes [1] .tagName). This is all the content of the article "what are the common HTML parsing libraries under JS?" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.