Teach to use the parser. Everyone knows the site yandex.ru . I was able to write a program that takes the source code of this site. I can not write a parser that takes weather from this site. Straight from the main page. I need only three characters, for example: +25. All source code is written to the variable NSString *str .

PS: Anyone can see the source code of the main page of Yandex. So, it is written in the string variable str . Here I need to unzip it and get only characters with weather into another string variable. The parser code is the source code for Yandex. But a little necessary code

 NSError * error = nil; HTMLParser * parser = [[HTMLParser alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.yandex.ru"] error:&error]; if (error) { NSLog(@"Error: %@", error); [parser release]; parser = nil; return; } HTMLNode * bodyNode = [parser doc]; // получаем родительский элемент HTMLNode *mynode = [bodyNode findChildWithAttribute:@"id" matchingName:@"all" allowPartial:TRUE]; // Берем div с id="all" NSArray *newsList = [mynode findChildTags:@"li"]; // получаем NSArray HTMLNode элеметнов содержащих li тэги с новостями на главной for (HTMLNode *news in newsList) { // Пробегаем по списку тэгов и выводим новости NSLog(@"Новость: %@", [news allContents]); // выводим текст из HTMLNode news и всех children } [parser release]; // освобождаем память 
  • You already created a topic about parsing XML, and adequate answers were given there. - Tuggen
  • Anyway, how did you get this XML? Maybe you still pumped out HTML ?? - Tuggen
  • Perhaps. The source code is in fact html? - Sergey4590
  • one
    Questions because such, you set the wrong task. What for to download large and complex including. for parsing html (which, by the way, is not intended for it, unlike xml, which serves to transfer data), if the number you need, which itself gets there from another place and getting it from there is most likely easier - aknew
  • one
    I know, I've been watching him here for a week already :) I like that any answer immediately appears in the form of a question, even that which is the standard structure of the language - aknew

2 answers 2

I need only three characters, for example: +25. All source code is written to the variable NSString * str.

As I understood from this line, that all HTML content is simply stored in the variable str , which is NSString . If I understand you correctly, in the line you need to search for tsiferki. If so, then you can add a category, that is, something like a private method to NSString : for example, we have <pogoda>+25</pogoda> , now we just need to extract the value between the tags:

  1. Create a category to NSString
  2. .h file
 -(NSString*)stringBetweenString:(NSString *)start andString:(NSString *)end; 
  1. .m file

     -(NSString*)stringBetweenString:(NSString*)start andString:(NSString*)end { NSScanner* scanner = [NSScanner scannerWithString:self]; [scanner setCharactersToBeSkipped:nil]; [scanner scanUpToString:start intoString:NULL]; if ([scanner scanString:start intoString:NULL]) { NSString* result = nil; if ([scanner scanUpToString:end intoString:&result]) { return result; } } return nil; } 
  2. using

NSString * query = [theHTML stringBetweenString: @ "<pogoda>" andString: @ "</ pogoda>"];

how many "weather" there are as many values ​​and will pull out (use in its class).

  • If you inspect the element, here’s where the weather is a class = "b-weather__link" href = " pogoda.yandex.ru/samara " onmousedown = "cp ('v11.weather.title', this)"> Weather <img class = " b-weather__icon "title =" cloudy with clearings "alt =" cloudy with clearings "src =" img.yandex.net/i/wiz6.png "/> </a> +22 <-------- --------------------------- HERE </ span> - Sergey4590
  • Explain why the category and the files are created? Can I just write this in the code? - Sergey4590
  • From the site netcoder.ru "The category is used if you want to add behavior to a class without inheriting from it. That is, the category allows you to extend the class and add new methods to it without requiring source code for the class, as well as the added methods automatically become available to all classes inherited from the changeable. As usual, you start by creating a pair of files containing the @interface interface and the @implementation implementation. The category has its own name, list of methods, and the name of the class it expands. " - x86-64
  • one
    When you create a category, you will have two files .h and .m, so I painted everything in such detail. - x86-64
  • one
    And how do you add classes to the project? New File-> Cocoa Touch-> Objective-c category further give a name and indicate what category to nsstring. Then include in your class and use as I described above. Sergey4590 and for which I was fined ??? - x86-64

Example here: XmlPullParser. Parsim XML .

  • And what does Android have to do with ???? - Tuggen