简体   繁体   中英

How to solve "CORS" while trying to scrape sites like Google.com using JavaScript?

I m trying to scrape google.com (just for fun) using JQuery Ajax.

Mostly by fetching the whole site into a var and then stripping out necessary tags from it.

However, it kinda works for normal sites, but when I tried google.com, it passed a CORS issue.

how can I solve this if I had no control over the client site or hosting?

ie, can't place header('Access-Control-Allow-Origin: *');

My code goes as:

$.ajax({
     url: "https://www.google.com/",
     dataType: 'text',
     success: function(data) {
          var title = $("<div>").html(data)[0].getElementsByTagName("title")[0];     
            console.log(title);
     }
});

Error: Access to XMLHttpRequest at 'https://www.google.com/' from origin 'https://xxxxx.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

Any help is greatly appreciated.

I have resolved it by installing CORS extension in my default browser. For that, I use the Google chrome extension in link below : https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en

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