How can I clear a webpage using jQuery and XPath?

I can insert javascript jQuery link into web page header via Firebug. Then I can run the script to clear it and the pages it links to.

How to start writing this script in jQuery or javascript in general? Is there an interface in jQuery / Javascript with which I can use XPath to access the elements on the page (and on the pages that it refers to)?

+4
source share
3 answers

First you need a JavaScript runtime environment outside the browser. The most common is Node.js. Then you need a way to create the client part of the DOM. This is usually done using jsdom .

So your script should:

  • load the html page ( jsdom does this for you, but you can use request )
  • create a client-side DOM
  • jQuery analysis

Here is an example Node.js script:

 var jsdom = require("jsdom"); jsdom.env("http://nodejs.org/dist/", [ 'http://code.jquery.com/jquery-1.5.min.js' ], function(errors, window) { console.log("there have been", window.$("a").length, "nodejs releases!"); }); 

You run it like this:

 $ node scrape.js 

Remember to install jsdom first:

 $ npm install --production jsdom 
+5
source

You can quickly get the HTML page:

 var html = document.documentElement.innerHTML; 

This will only return a string literal and will not capture the root element.

+3
source

You may be interested in pjscrape , a web clip library built just for this purpose (disclaimer: this is my project). It is based on PhantomJS , a mute Webkit implementation that you can run from the command line, and has a very simple syntax for clearing data from multiple pages and finding additional URLs for spiders and scratches.

+3
source

Source: https://habr.com/ru/post/1400401/


All Articles