2015-04-04 21 views
0

因此,我經常與一家公司合作,並將他們的網站外包給另一家公司進行管理。我試圖將網站的數據放入我的應用程序中以填充UITableViewController,但我無法找到該網站提供的HTML頁面的源頁面(http://www.pointstreak.com/prostats/scoringleaders.html?leagueid=49&seasonid=12983)。我使用了Chrome的開發工具,並以JSON的方式找到了網站的表格數據(我想,我不確定)。它開始是這樣的:如何在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中嗎?

此外,是否有無論如何保持這個飼料我的應用程序的現場版本,作爲一個簡單的複製和粘貼將保持靜態?謝謝。

+0

如果我理解正確的話,你想獲得創建這個網站的一個AJAX網址。在網絡選項卡中的開發人員控制檯中,您可以篩選xhr請求,然後您會看到對以下網址的ajax調用:[http://www.pointstreak.com/ajax/trending_ajax.html?action=leaguescoreboard&leagueid=49] (http://www.pointstreak.com/ajax/trending_ajax.html?action=leaguescoreboard&leagueid=49)。 – AWolf 2015-04-04 23:51:34

+0

會不會有另外一個這樣的頁面,但其中包含填充「Scoring Leaders」表格的信息? – Jay 2015-04-04 23:59:40

+0

我認爲頁面的其餘部分是用severside腳本完成的。看起來好像領導者表被插入到php腳本標記中。請參閱scoringleaders.html的源代碼行318。該行以'var team_source = [{label:「Bloomington(USHL 2014/2015)」,...' – AWolf 2015-04-05 00:30:52

回答

0

首先,你應該使用我的代碼僅用於開發,如果你正在使用網站創建者。它看起來有點像從他們的頁面竊取數據。

如果他們可以在其後端實現JSON或JSONP請求,那麼您可以添加url參數&format=jsonp會更好。

任何方式,下面是從頁面獲取數據的腳本。腳本的細節見上面我的評論。您也可以在jsfiddle找到它。

如果不存在JS對象,你也可以解析HTML標籤中的數據,但這有點複雜,但也有可能。

(如果你在相同的起源,你並不需要HTTP代理。)

這不是得分表,但該信息的球隊(名稱+網址球隊信息頁)。 評分表是使用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>

+0

我打算與網站創作者交談,但不知道我要求他們做什麼。如果我要求他們「實現JSON或JSONP請求到他們的後端,所以我可以添加一個url參數,比如&format = jsonp」,我只需要將它添加到url中,然後我就可以獲得JSON數據,然後可以通過Xcode把它變成一個NSArray/Dictionary?順便說一句,非常感謝。 – Jay 2015-04-06 16:16:22

+0

是的,這應該工作,但我不知道Xcode。你可以看看其他可用API來獲取你所需要的API。例如看看[這](http://en.wikipedia.org/w/api.php?format=json&action=query&titles=Main%20Page&prop=revisions&rvprop=content&rawcontinue=true)json響應這是從維基百科。你可以在這裏找到api的詳細信息(http://www.mediawiki.org/wiki/API:Main_page#A_simple_example)。一個不錯的頁面也是[http://jsonplaceholder.typicode.com/](http://jsonplaceholder.typicode.com/)。 – AWolf 2015-04-06 16:40:05

+0

謝謝,我正在看那些。因此,如果我問網站創作者是否實施了這個請求,那麼對他們來說會有很多工作嗎?我發現的另一個網站(網站,而不是應用程序)已經從我上面提供的網址獲取數據。 (請參閱我將在另一條評論中發佈的鏈接。) 由於這個其他網站已經得到它,這是否意味着必須實施的東西我不知道,有一種方法,我可以很容易地得到它JSON? (我需要來自網站許多不同部分的數據;我與網站創建者安排了一次會議,但他們的工作越少越好。) – Jay 2015-04-06 16:49:41

相關問題