How to load HTML site code using jquery

How can I load a website into my java-script so that I can parse it? I want to get Html, for example. www.google.com and I want to select all the tags in it using jquery.

+5
source share
3 answers

You cannot, as jquery does not allow loading external resources, unless there is a heading on the page you want to analyze:

header('Access-Control-Allow-Origin: http://thesitewhereyourjscodeishosted'); 

If you cannot install it, you can use PHP:

 <script> var website = <?php echo file_get_contents("http://websitetoload"); ?>; </script> 
+4
source

Due to browser security restrictions, Ajax requests are subject to the same origin policy; The request cannot be successfully retrieved from another domain, subdomain, port, or protocol.

But you can create a script on your server that requests this content or can use a proxy server and then use jQuery ajax to access the script on your server.

Working script

It simply proxies the request through Yahoo servers and returns a JSONP response, even if the requested server does not support JSONP .

HTML:

 <div id="example"></div> 

Javascript

 $('#example').load('http://wikipedia.org'); 

Here is the same question as yours How to circumvent a policy of the same origin

Good luck

+2
source

You can easily configure the node server, which receives the contents of the page, and then make an ajax request to your server and get all the necessary data.

0
source

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


All Articles