如何在Chrome中找到JSON后将其放入我的应用程序's开发人员工具
How to get JSON into my app after I locate it in Chrome's Developer tools?
所以我经常与一家公司合作,他们将网站外包给另一家公司管理。我正试图将网站的数据拉入我的应用程序以填充UITableViewController,但我找不到该网站提供的html页面的源页面(http://www.pointstreak.com/prostats/scoringleaders.html?leagueid=49&季节性=12983)。我使用了Chrome的开发工具,并在JSON中找到了该网站的表数据(我想,我不确定)。它的开头是这样的:
<table width="98%" class="tablelines" cellpadding="2" border="0" cellspacing="1">
<tr>
<td colspan="17" class="maincolor">
<a href="scoringleaders.html?leagueid=49&seasonid=12983&position=D">View Defense</a> | <a href="scoringleaders.html?leagueid=49&seasonid=12983&position=F">View Forwards</a> | <a href="scoringleaders.html?leagueid=49&seasonid=12983">All Positions</a> </td>
</tr>
<tr align="center" class="tableheader">
<td width="2%">RK</td>
<td align="left" width="20%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=name">PLAYER</a></td>
<td width="6%">TEAM</td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=pos">Pos.</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=gp">GP</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=g">G</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=a">A</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983">PTS</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=plusminus"> +/-</a></td><td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=pim">PIM</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=ppg">PP</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=ppa">PPA</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=shg">SH</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=sha">SHA</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=gwg">GWG</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=shots">SHOTS</a></td>
<td width="6%"><a class="sort" href="scoringleaders.html?leagueid=49&seasonid=12983&sortby=spct">S%</a></td>
</tr>
<tr align="center" class="maincolor">
<td class="light">
1 </td>
<td class="light"align="left"><a href="playerpage.html?playerid=7208177&seasonid=12983">Johnson, Adam</a></td>
<td><a href="teamplayerstats.html?teamid=3138&seasonid=12983">SC</a></td>
我认为它是JSON。现在,由于这只是在Chrome的开发工具中,而不是当我输入某个URL时页面上出现的内容,有什么方法可以将这些数据加载到我的tableView中吗?
此外,有没有办法保持我的应用程序的实时版本,因为简单的复制和粘贴可以保持它的静态?谢谢
首先,如果您正在与网站创建者合作,您应该只在开发时使用下面的代码。在我看来,这有点像从他们的页面上窃取数据。
如果他们能在后端实现对JSON或JSONP的请求,这样你就可以添加像&format=jsonp
这样的url参数,那就更好了。
无论如何,下面是从页面获取数据的脚本。有关脚本的详细信息,请参阅我在上面的评论。你也可以在jsfiddle上找到它。
如果没有JS对象,您可能还可以解析HTML标记中的数据,但这有点复杂,但也是可能的。
(如果你在同一个原点,你不需要http代理。)
这不是分数表,而是球队的信息(球队信息页面的名称+url)。分数表是用PHP创建的。您可以看到这一点,因为如果您在表中使用过滤器,它会向后端创建一个新的请求。
我认为最好的方法是与网站创建者交谈,检查他们是否可以为您实现JSON(p)请求的API。因此,您可以轻松创建一个前端应用程序,而无需篡改其中的数据。
更新2015年4月6日(12:12 UTC)
HTML标记的提取是可能的——请参阅这个jsFiddle。如前所述,情况更为复杂。
但我认为最好的方法是使用生成的JSON,并使用类似mocky.io的模拟服务,因为如果你只需要它来测试你的应用程序,它会更快、更好。
$.ajax({
url: 'https://cors-anywhere.herokuapp.com/http://www.pointstreak.com/prostats/scoringleaders.html?leagueid=49&seasonid=12983',
method: 'GET',
success: function (data) {
//console.log(data);
regEx = /var team_source's*='s*'[([^']]+)]/; // regex to filter the team_source variable
// now find the js object with the braces [ ]
console.log(regEx.exec(data));
eval(regEx.exec(data)[0]);
//var team = new Object(team_source);
console.log(team_source[0].label); // now you can work with the js object
$('#output').html(JSON.stringify(team_source, null, 2));
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<pre id="output"></pre>
- 我在哪里从 React Redux 应用程序中的服务器获取初始数据
- 有没有一种方法可以在IE8中解决我的Rails javascript应用程序.js的问题
- 获取包含我的谷歌应用程序脚本的页面的url
- 胡子.js卡在我的 Rails 应用程序中的 0.4.0-dev 上
- 如何消除我的应用程序上的希腊字符
- 我的应用程序中的可点击元素应始终是锚标记
- 如何创建重用我的 JS 库的 iPad 应用程序,这些库是为 Web 应用程序构建的
- ASP.NET:是否可以将CSS或Javascript注入到我的Web应用程序中的每个页面中
- 为什么IE无法正确显示我的HttpHandler Web应用程序中的特殊符号
- 为什么我的应用程序中的某些组件在IE7中工作,而在IE9中不起作用
- 为什么我的Mac应用程序中的WebView html拒绝加载JQuery或任何外部脚本
- Angularjs模式窗口在我的应用程序中的实现
- 我曾经跟随某人的代码样本,并尝试应用到我的HighChart程序,但我可以'I don’我一点也做不到
- 如何从我的web应用程序中的Java脚本文件中获得加密的密码
- 如何始终解析相对于我的web应用程序根的URL
- 我可以使用标准Javascript应用程序中的Google Polymer吗
- Facebook应用程序-我如何计算发件人邀请的朋友数量
- 访问导致我的应用程序崩溃的数组元素
- 我的网络应用程序中的谷歌地图API
- Javascript应用程序-我的代码怎么样