简体   繁体   English

如何在尝试使用 JavaScript 抓取 Google.com 等网站时解决“CORS”问题?

[英]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.

相关问题 载入google.com时Javascript报告CORS错误 - Javascript reporting CORS error when loading google.com 如何在 JavaScript 中将带有 css 的弹出窗口添加到现有网站(例如 google.com)? - How to add a popup with css to an existing website (such as google.com) in JavaScript? 如何通过javascript或jQuery将iframe中的所有链接设置为“ google.com”? - how to set all links in iframe to “google.com” by javascript or jQuery? Javascript和Imacro打开Goog​​le.com - Javascript and Imacro to Open Google.com 如何在谷歌地图中突出显示用户的当前位置? (就像新的 google.com/maps PWA) - How to highlight a user's current location in google maps? (Like the new google.com/maps PWA) Google.com和DOMContentLoaded - Google.com and DOMContentLoaded 在网址上运行JavaScript函数? google.com?javascript:(func()) - running JavaScript function on url? google.com?javascript:(func()) 验证网址JavaScript以接受网址条目google.com - Validate web url JavaScript to accept url entry google.com 使用Chrome扩展程序将jQuery注入Google.com - Injecting jQuery into Google.com using a Chrome Extension 使用google.com/tbproxy/spell进行拼写检查 - spell check using google.com/tbproxy/spell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM