2013-01-21 61 views
1

我想從一個網站獲取數據到表視圖使用Hpple包裝:https://github.com/topfunky/hpple從一個網站獲取數據到一個表視圖

在網頁源代碼中我看到(這是相關部分):

<div id="traffic_content"> 
       <marquee width="412" height="21" direction="right" scrollamount="3"> 
        <a href="/Site/Traffic.aspx" id="ctl00_TrafficHolder">מעודכן לשעה: 16:40&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;בכביש תל אביב חיפה הישן, עומס תנועה ממחלף כפר סבא רעננה צפון עד מחלף הדרים, בגלל תנאונת דרכים.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;כביש מספר 70 עמוס מצומת אבליים עד צומת יבור.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;איילון דרום עמוס ממחלף רוקח עד מחלף לה גווארדיה. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;איילון צפון עמוס ממחלף חולון עד מחלף גלילות מזרח. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;***&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;שימו לב, בעלי זכות בחירה אשר יימצאו ביום הבחירות בישוב המרוחק למעלה מ-20 קילומטר מתחום השיפוט של הישוב בו ממוקם הקלפי בו הם אמורים להצביע, זכאים לנסיעה חינם בתחבורה הציבורית &#40;אוטובוסים והרכבת&#41;.יש להציג תעודה מזהה ואת ההודעה לבוחר.&#40;ממשו את זכותכם להצביע&#41;.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;למסירת ולקבלת דיווחים ותזמונים חייגו: 918 - 800 - 1-800&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;בנסיעה בקרבת בתי ספר, גינות משחקים ומתנ&quot;סים – יש להוריד מהירות, גם כשהכביש פנוי. בהגיעכם למעבר חצייה – אפשרו תמיד חצייה לילד המבקש לחצות. היו דרוכים, ערניים ומרוכזים, וחפשו אתם את הילדים העשויים להתפרץ לכביש.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;עורך דיווחי התנועה:בני כבודי</a> 
       </marquee> 
      </div> 

我很感興趣,讓裏面的內容到一個表視圖。

所以,我想:

NSURL *trafficUrl = [NSURL URLWithString:@"http://www.glgltz.co.il/Site/Traffic.aspx"]; 
NSData *trafficHtmlData = [NSData dataWithContentsOfURL:trafficUrl]; 

TFHpple *trafficParser = [TFHpple hppleWithHTMLData:trafficHtmlData]; 

NSString *trafficXpathQueryString = @"//div[@id='traffic_content']/a"; 
NSArray *trafficNodes = [trafficParser searchWithXPathQuery:trafficXpathQueryString]; 

NSMutableArray *newTraffic = [[NSMutableArray alloc] initWithCapacity:0]; 
for (TFHppleElement *element in trafficNodes){ 
    Traffic *traffic = [[Traffic alloc] init]; 
    [newTraffic addObject:traffic]; 
    traffic.name = [[element firstChild] content]; 
} 

然後將其加載到表視圖的NSArray的。

當我調試代碼時,我看到trafficNodes中有0個對象。

如何正確獲取這些數據?

+0

你使用webView組件,或者你想添加外部Safari瀏覽器的數據? – pbibergal

+0

如果我理解你正確,我既不使用。我只是想將網站中的數據解析爲tableview,並且解析它時出現問題。 – oridahan

回答

1

你忘了你的XPath的標籤;)的NSArray的是空的,因爲沒有"div[@id='traffic_content']/a"

嘗試進行如下修改:

@"//div[@id='traffic_content']/marquee/a" 

我試着用下面的代碼,一切都在下載似乎工作;)

//EDIT: //After converting trafficHtmlData the both the label and NSLog() show correct hebrew letters 
    NSData *trafficHtmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.glgltz.co.il/Site/Traffic.aspx"]]; 
    NSString *trafficHTMLDataString = [[NSString alloc] initWithData:trafficHtmlData encoding:NSUTF8StringEncoding]; 
    NSData *newData = [trafficHTMLDataString dataUsingEncoding:CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingWindowsHebrew)]; 
    if (trafficHtmlData != nil) { 
     TFHpple *trafficParser = [TFHpple hppleWithHTMLData:trafficHtmlData]; 
     NSArray *trafficNodes = [trafficParser searchWithXPathQuery:@"//div[@id='traffic_content']/marquee/a"]; 
     TFHppleElement *element = trafficNodes[0]; 
     //EDIT: Convert the first element in trafficNodes 
     NSString *string = [element content]; 
     [self.label setStringValue:string]; 
     NSLog(@"%@", string); 

     /* 
     NSMutableArray *newTraffic = [[NSMutableArray alloc] initWithCapacity:0]; 
     for (TFHppleElement *element in trafficNodes){ 
      Traffic *traffic = [[Traffic alloc] init]; 
      traffic.name = [[element firstChild] content]; 
      [newTraffic addObject:traffic]; 
     } 
     */ 
    } 
+0

它確實有效,我在trafficNodes中有一個對象,問題是因爲我看到的是希伯來字符,所以我看到的是unicode字符,當我嘗試轉換時,我主要看到:「2013-01-22 15:28:16.749例如,當我嘗試轉換「\ u05f3 \ u2022」時,HTMLParsing [10002:c07]結果字符串:「•」。任何想法如何將其正確轉換爲顯示希伯來字符? – oridahan

+0

看看我編輯的答案。你有沒有嘗試過或如何轉換你的NSString? – HAS

相關問題