+ 1

How to get a page and feed it to a parser on JQuery???

I wrote a site page parser at JQuery, and now I need to some how get this page and feed it to the parser. There are also limitations of the libraries used. IN html коде I painted them. https://code.sololearn.com/WR4lRaILDe5k/?ref=app

11th Mar 2021, 11:43 AM
just@man
just@man - avatar
1 Resposta
+ 2
Generally, you can use $.ajax or JavaScript's native fetch function to send an HTTP request anywhere and download anything you want. The main problem here is that your HTTP request crosses domains from one in sololearn.com to foreca.com and foreca.com doesn't respond with the CORS header to allow JavaScript to download it from other domains. This means, the browser will enforce the CORS policy by preventing your JavaScript from reading the HTML from foreca.com. The simplest solution is to relay or proxy your HTTP request through a server that adds the necessary CORS header. https://cors-anywhere.herokuapp.com is popular for this but it doesn't always work. You could host your own alternative. Many server-side programming languages could implement this relay but you'll need to host outside of sololearn.com. You could get a free account with 000webhost.com and implement it in PHP. Something like this could become your relay script: (?php // replace the curved bracket. Sololearn doesn't let me save an answer with the correct php start indicator. header("Access-Control-Allow-Origin: *"); echo file_get_contents('https://www.foreca.com'); You could then write your JavaScript to hit your relay script like this: fetch('https://000webhost.com/foreca_relay.php').then(function (response) { return response.text(); }).then(function (html) { // This is the HTML from our response as a text string console.log(html); }).catch(function (err) { // There was an error console.warn('Something went wrong.', err); }); You might be interested in this weather code: https://code.sololearn.com/W8XpSCS9yfra/# It apparently relies on https://cors-anywhere.herokuapp.com and another weather website. It hits the weather API instead of parsing and scraping from HTML.
11th Mar 2021, 3:06 PM
Josh Greig
Josh Greig - avatar