简体   繁体   中英

Parse html of current page

I want to make a script that will parse the html of the current page, filtering out certain div classes and for now to write their contents to a file or remove everything else but them on the page.

I guess the best way would be to run a Tampermonkey script on that page. I looked on http://userscripts-mirror.org/ but didn't find a script like this.

Is there a javascript html parser that can run on chrome?

Something that could work like this maybe?

 var divClasses = parseCurrentPage("div class x");
 // then do something on divClasses and then show only them

filtering out certain div classes

You can use document.getElementsByClassName()

 var elements = document.getElementsByClassName(names); // or: var elements = rootElement.getElementsByClassName(names); 
  • elements is a live HTMLCollection of found elements.
  • names is a string representing the list of class names to match; class names are separated by whitespace
  • getElementsByClassName can be called on any element, not only on the document. The element on which it is called will be used as the root of the search.

Jquery can do all of this and more. I would recommend reading up on it https://learn.jquery.com/

Once you have included jquery a simple grab all "div" elements selector would be something like this: var divClasses = $('div'); If you want to only grab certain div elements you can easily do this using selectors, either by adding class, id, and/or parent/hierarchy level restrictions to the selector - read more here https://api.jquery.com/category/selectors/

Then after you did your something you want to do on the div elements you can again use jquery to only show them using jquery's 'append' function.

Simple call the 'append' function with the div element you want to append on the parent html element $('selector-to-grap-div\\'s-parent-html').append(myDiv); if you need to grab the parent of one of the div elements then you can use jquery's 'parent' function

我相信您可以使用jQuery轻松实现...只需获取<body>的内容并使用jquery进行查询。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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