简体   繁体   English

如何从具有 AJAX 请求的网站获取 HTML 页面而不出现 CORS 错误?

[英]How do I get HTML page from a website with AJAX request without getting CORS error?

I'm trying to do basic webscraping with javascript code using XMLHttpRequest.我正在尝试使用 XMLHttpRequest 对 javascript 代码进行基本的网页抓取。 The code is triggered when I click on a button in an html page that I open with my Firefox browser.当我单击使用 Firefox 浏览器打开的 html 页面中的按钮时,将触发代码。

The code I'm using is below.我正在使用的代码如下。 It tries to get the html page from ATP tour website and to print it as first step.它尝试从 ATP 旅游网站获取 html 页面并将其作为第一步打印。 Then I will try to parse it to get the information I want (like a rank for example).然后我会尝试解析它以获得我想要的信息(例如排名)。

let htmlRequest = new XMLHttpRequest();
htmlRequest.open("GET", "https://www.atptour.com/en/players/matteo-berrettini/bk40/overview");
htmlRequest.onreadystatechange = function () {
    if (this.readyState == XMLHttpRequest.DONE && this.status == 200) {
        console.log(this.responseText);
    }
};
htmlRequest.send();

I discovered that running this code on localhost does not work because I'm not on the same domain as the site I'm sending my request (it is the classical CORS issue).我发现在 localhost 上运行此代码不起作用,因为我与发送请求的站点不在同一个域中(这是经典的 CORS 问题)。

I now understand I need to create a web server but my understanding of the problem stops here.我现在明白我需要创建一个 web 服务器,但我对问题的理解到此为止。

So my question is: what do I have to implement to make my js script working?所以我的问题是:我必须实现什么才能使我的 js 脚本正常工作?

Have you tried a web-scraping library?您是否尝试过网络抓取库? These are a few:这些是一些:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不看到 CORS 错误的情况下从网站 URL 获取图标? - How to get a favicon from a website URL without seeing a CORS error? 如何获得从HTML到Ajax请求的选定复选框? - How do I get selected checkboxes from HTML to Ajax request? 如何从 AJAX 向 Odoo 10 自定义模块 controller 发出 POST/GET 请求? (被 CORS 政策阻止) - How do I do POST/GET request from AJAX to Odoo 10 custom module controller? (Blocked by CORS policy) 如何在不编码字符串的情况下将字符串作为查询参数传递给 Ajax GET 请求? - How do I pass a string to an Ajax GET request as a query param without the string getting encoded? 如何在没有跨站点请求错误的情况下将html加载到页面中 - How to load html into page without getting Cross Site Request error 如何使用 jsPDF 和 HTML2Canvas 从网站获取多页 pdf? - How do I get a multi-page pdf from a website using jsPDF and HTML2Canvas? 从网站获取内容时如何修复 CORS 错误? - How to fix CORS error when getting contents from a website? 如何在没有HTML页面的情况下获得Ajax响应? - How to get ajax response without html page? 如何控制 HTML 页面在 GET 请求中返回的内容 - How do I control what a HTML page returns on a GET request 从HTML页面发起CORS请求 - Initiating a CORS request from a HTML page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM