简体   繁体   English

在没有API的情况下从网站获取数据/创建API

[英]Getting data from a website without API / Creating an API

I need to get some data from a Website (the user will change). 我需要从网站获取一些数据(用户将更改)。 However there is no 'good' API for Overwatch Stats. 但是,没有“监视”统计信息的“良好” API。 Is it possible in Javascript (Using Node.js) to get this data by the HTML tags or of sort? 在Javascript中(使用Node.js)是否可以通过HTML标记或某种形式获取此数据?

For example, this is one of the lines in the source code: 例如,这是源代码中的行之一:

<span class="summary-hero-name">McCree</span>

And the 3 hero's on display on the page each have the class summary-hero-name , is there any way to reference this data? 并且页面上显示的3位英雄各自具有summary-hero-name ,有什么办法可以引用此数据?

So I figured this out, should have done a bit more research. 所以我想通了,应该做更多的研究。 I completed this by using a Node Package called scrape-it . 我通过使用名为scrape-it的Node Package来完成此操作。

<scrape-it>("https://masteroverwatch.com/profile/pc/us/calvin-1337", {
  title: "span.summary-hero-name"
}).then(page => {
  console.log(page); // {title: 'McreeWidowmakerBastion' }
});

I believe this post will be helpful. 我相信这篇文章会有所帮助。 I thought to use AJAX to make a request like this:... 我想使用AJAX发出这样的请求:...

<html>
<head>
<script type="text/javascript">
function go(){

var xhr = new XMLHttpRequest();

xhr.open("GET", "https://masteroverwatch.com/profile/pc/us/calvin-1337", false);
xhr.onreadystatechange = function(){

  if (xhr.readyState == 4) 
  {
    document.write(xhr.responseText);
  }
  else
  {
    document.write("nope");
  }
}

xhr.send();}

</script>
</head>
<body onload="go()">

</body>
</html>

Normally this would work if it was on the same domain, however, I got the error: "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access". 通常,如果它在同一域上,则可以使用,但是,我收到错误消息:“所请求的资源上没有'Access-Control-Allow-Origin'标头。因此,不允许访问源'null'”。 This is apparently a security feature. 这显然是一项安全功能。 Seems like Cross-Origin Resource Sharing (CORS) is the way to go. 好像跨域资源共享(CORS)是要走的路。 Good luck. 祝好运。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM