oridahan
oridahan

Reputation: 574

Get data from a website into a Tableview

I'm trying to get data from a website into a table view using the Hpple wrapper: https://github.com/topfunky/hpple.

In the website source code I see (which is the relevant part):

<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>

I'm interested In getting the content inside into a table view.

So I tried:

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];
}

and then load it to the NSArray of the table view.

When I debug the code I see that there are 0 objects in trafficNodes.

How can I get this data correctly?

Upvotes: 1

Views: 203

Answers (1)

HAS
HAS

Reputation: 19853

You forgot the tag in your XPath ;) The NSArray is empty because there is no "div[@id='traffic_content']/a"

Try to modify it as follows:

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

I tried downloading it with the following code and everything seems to work ;)

    //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];
         }
         */
    }

Upvotes: 1

Related Questions