2012-08-04 64 views
1

任何机构都可以帮助我, 我想从网站解析,因为我是新的目标C,我不知道该怎么办,是否有任何示例代码,我可以看到得到一些想法?从网络解析

+0

要分析是什么? Html/xml或二进制数据?你想从流中解析它,还是想先下载它? – Tutankhamen 2012-08-04 15:38:48

+0

我想分析视频的链接,并解析新闻的链接。然后在表格中显示视频的链接,并在不同的表格中显示新闻的链接。 – Hamid 2012-08-04 16:28:42

+0

在这种情况下,你最好使用ASIHTTPRequest库...(见下文)。 – Tutankhamen 2012-08-04 17:02:12

回答

1

试试这个:http://allseeing-i.com/ASIHTTPRequest/

- (IBAction)grabURLInBackground:(id)sender 
{ 
    NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"]; 
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
    [request setDelegate:self]; 
    [request startAsynchronous]; 
} 

- (void)requestFinished:(ASIHTTPRequest *)request 
{ 
    // Use when fetching text data 
    NSString *responseString = [request responseString]; 

    // Use when fetching binary data 
    NSData *responseData = [request responseData]; 
} 

- (void)requestFailed:(ASIHTTPRequest *)request 
{ 
    NSError *error = [request error]; 
} 
+0

ASI很好,但不再支持。尝试MBRequest! https://github.com/mobiata/MBRequest – logancautrell 2012-08-05 17:39:36

+0

我不知道为什么我的问题投下来!我不能再问一个问题:( – Hamid 2012-08-18 04:31:00

0

你的问题是非常不明确的,因为该方法取决于你想要解析哪种数据。

如果你只是想填补你的网站的内容的字符串,你可以使用:如果你想解析XML或JSON文件有喜欢的NSXMLParser类

NSString *foo = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.apple.com"] encoding:NSASCIIStringEncoding error:nil]; 

+0

切勿从主线程调用该方法。它会阻止并可能杀死你的应用程序。 – logancautrell 2012-08-04 15:27:02

+0

我想解析的网站有视频链接和新闻链接,我想解析这些链接 – Hamid 2012-08-04 15:39:44

+0

由于@logancautrell表示您需要第二个线程来执行此操作。 '[self performSelectorInBackground:@selector(readThread)withObject:[NSThread currentThread]];'作为下一步你可能会阅读这篇文章[链接](http://stackoverflow.com/questions/3020849/simple-libxml2-html- parsing-example-using-objective-c-xcode-and-htmlparser-h)关于Xcode中的html解析的一些信息 – ChoboDev 2012-08-04 16:43:49