[英]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.我正在尝试使用 JQuery Ajax 抓取 google.com(只是为了好玩)。
Mostly by fetching the whole site into a var and then stripping out necessary tags from it.主要是通过将整个站点获取到一个 var 中,然后从中删除必要的标签。
However, it kinda works for normal sites, but when I tried google.com, it passed a CORS issue.但是,它适用于普通网站,但是当我尝试 google.com 时,它通过了 CORS 问题。
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: *');
即,不能放置
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.
错误:
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.我已经通过在我的默认浏览器中安装 CORS 扩展来解决它。 For that, I use the Google chrome extension in link below : https://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en
为此,我在下面的链接中使用 Google chrome 扩展: https ://chrome.google.com/webstore/detail/allow-cors-access-control/lhobafahddgcelffkeicbaginigeejlf?hl=en
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.