Recently in the study of the implementation of the iphone water layout, first of all have to have data, so I just casually grab some data on the internet to achieve. There are a lot of web sites are used waterfall flow, such as Mushroom street, petal nets, Beautiful said, and so on, today, as an example of petal nets, try to grab the data we want inside.
Originally I want to use objective-c an HTML open-source framework Hpple to parse the HTML data returned by the petal net, extract the nodes we want, but I tried a bit, can not realize, also listen to the internet said this framework is not very perfect. So I simply use tools to achieve, there are a lot of such tools on the Internet, now I first a call Charles's HTTP proxy monitoring tool, it can listen to our HTTP request and data return. Install Charles see this software installation directory has a lot of jar package, then this software must be written in Java. Also, the Java language is very mature, with a very large number of people, the network also has many open-source network parsing framework, such as Htmlparser.
Don't say much nonsense, go to the subject immediately. OK, open Charles, see what this software looks like and also open the browser, I need to crawl the data address is: http://huaban.com/daoquer/pins/
OK, intercept to the data,, the response inside the entire contents of the copy, save to TXT file, space is limited, I took some of the JSON content needed to intercept the road, pay attention to the red lines, it is I want to extract the data, note to choose the Utf-8 encoding format.
"Pins": [{
"pin_id": 43990618,
"user_id": 811487,
"board_id": 2203828,
"file_id": 10738872,
"File": {
"Farm": "Farm1",
"Bucket": "Hbimg",
" Key": "2746e1395bacc6763dadbcc823fe4101fd6814ec37aab0-vlbgal",
"type": "Image/gif", //Picture format
"Frames": 46
},
"Media_type": 0,
"Source": "Daoquer.com",
"Link": "http://daoquer.com/i/1407",
" Raw_text": "Playing skateboard is good fun to be, scraping flowers and grass is not good!" ",//Picture description information
"Text_meta": null,
}
.......
.......
]
Because I was developing with a virtual machine, I copied the files to the Mac system, opened Xcode, built a command line project, and entered the code.
#import<Foundation/Foundation.h>#defineBaseURL @ "http://img.hb.aicdn.com/"intMainintargcConst Char*argv[]) {@autoreleasepool {NSData*data = [NSData datawithcontentsoffile:@"/users/liwenshen/desktop/Petal Net/tao Fun/tao Fun child json.txt"];//generate NSData objects from parsed data /*JSON parsing online has a lot of open-source framework, such as Jsonkit,sbjson, are very useful, easy to use, I here on the original nsjsonserialization Apple. */nsdictionary*result =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments Error:nil]; //according to the contents of the dictionary to come up with the data we want, this JSON data is more complex, you need to observe the patientNsarray *array = result[@"User"][@"pins"]; Nsmutablearray*resultarr = [Nsmutablearray arrayWithCapacity:array.count];//used to temporarily save the last generated data for(Nsdictionary * dictinchArray) {Nsmutabledictionary*resultdict = [Nsmutabledictionary dictionary];//used to temporarily save the contents of an element[Resultdict setobject:dict[@"Raw_text"] Forkey:@"desc"];//a picture of the description information[Resultdict setobject:[nsstring stringWithFormat:@"%@%@", baseurl,dict[@"file"][@"Key"]] Forkey:@"Imgurl"];//The address of the picture, to add the base siteNSString *imgtype = [dict[@"file"][@"type"] Substringfromindex:6];//Intercepting string Strings[Resultdict setobject:imgtype Forkey:@"Imgtype"];//Picture Format[Resultdict setobject:dict[@"file"][@"width"] Forkey:@"width"];//the width of the picture[Resultdict setobject:dict[@"file"][@"Height"] Forkey:@"Height"];//the height of the picture//The width of the high by the server back, this way we don't have to forget, save a little things ~ ~[Resultarr addobject:resultdict]; } [Resultarr WriteToFile:@"/users/liwenshen/desktop/Petal Net/tao Fun/tao fun. plist"Atomically:yes];//for convenience, write to local//Download the picture, download the picture and download it later for(inti =0; I < resultarr.count;i++) {nsdictionary*dict =Resultarr[i]; NSString*imgurl = dict[@"Imgurl"]; NSData*data = [NSData datawithcontentsofurl:[nsurl Urlwithstring:imgurl]];//Download the network data, first download the image to use afterNSString *path = [NSString stringWithFormat:@"/users/liwenshen/desktop/petal Net/DAO fun child/images/%d.%@", i,dict[@"Imgtype"]]; [Data Writetofile:path Atomically:yes]; } } return 0;}
Run the project, plist files and pictures I saved to the/users/liwenshen/desktop/petals net/Road fun/, this path.
Well, this article introduces here, because each picture of the height is different, next to will be the flow layout want content to display to the interface.
iOS developed to crawl the petals net JSON data