KOYAMA Yoshiaki のブログ

プログラミングについての試行錯誤をつらつら書き溜めていきます。

Search2chBBS

プログラムは単純なものです。Core Data と WebKit を利用しています。WebKit を用いて URL [
http://menu.2ch.net/bbsmenu.html ] から bbsmenu.html ファイルをダウンロードしています。下にソースの一部を記載します。

_webView = [[WebView alloc] initWithFrame:NSZeroRect];
[_webView setFrameLoadDelegate:self];
[_webView setResourceLoadDelegate:self];

preferences = [_webView preferences];
[preferences setDefaultTextEncodingName:@"shift_jis"];

frame = [_webView mainFrame];
url = [NSURL URLWithString:@"http://menu.2ch.net/bbsmenu.html"];

request = [NSURLRequest requestWithURL:url];

[frame loadRequest:request];

それから WebKit を利用して DOM にアクセスしています。下に表示されているのが bbsmneu.html の一部とソースの一部分です。この場合は _category に 'PC等'、title に 'PCサロン' 、url に 'http://pc7.2ch.net/pc2nanmin/' などがそれぞれ対応しています。

<BR><BR><B>PC等</B><BR>
<A HREF=http://pc7.2ch.net/pc2nanmin/>PCサロン</A><br>
<A HREF=http://pc8.2ch.net/pcnews/>PCニュース</A><br>
<A HREF=http://pc8.2ch.net/win/>Windows</A><br>
<A HREF=http://pc8.2ch.net/jobs/>旧・mac</A><br>
<A HREF=http://pc7.2ch.net/mac/>新・mac</A><br>
if ([node hasChildNodes] == YES) {
if ([[node nodeName] isEqualToString:@"A"] == YES) {

    obj = [NSEntityDescription insertNewObjectForEntityForName:@"Board" inManagedObjectContext:[self managedObjectContext]];

    [obj setValue:_category forKey:@"category"];
    [obj setValue:[[node firstChild] nodeValue] forKey:@"title"];
    [obj setValue:[[[node attributes] getNamedItem:@"HREF"] nodeValue] forKey:@"url"];

} else if ([[node nodeName] isEqualToString:@"B"] == YES) {
    _category = [[node firstChild] nodeValue];
} else {
    [self nodeHref:[node childNodes]];
}