2011-10-27 27 views
0

我想解析一個網頁以獲取該頁面上的表格內容。但是,該表是通過調用document.write()和幾個javascript函數創建的。即當我加載該頁面的URL請求時,我得到的源代碼不包含該表的html標記。是否有可能獲得最終頁面的源代碼(即包含我想要的表格的html標籤的源代碼)?我非常好奇,我可以在Safari瀏覽器的開發工具中查看「最終頁面源代碼」,但不能查看頁面的來源。 這是頁面the source通過調用document.write創建解析表()

回答

0

您應該使用實現NSConnectionDataDelegate協議,並使用NSConnection類。我相信在調用-(void) connectionDidFinishLoading:(NSURLConnection *)connection時,頁面已完成加載。

-(void) requestPage 
{ 
    NSString *urlString = @"http://the.page.you.want.com"; 
    NSURL *url = [NSURL URLWithString:urlString]; 

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f]; 


    responseData = [[NSMutableData alloc] init]; 
    connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain]; 
    delegate = target; 
} 


-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    if ([response isKindOfClass:[NSHTTPURLResponse class]]) 
    { 
     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
     //If you need the response, you can use it here 
    } 
} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [responseData appendData:data]; 
} 

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [responseData release]; 
    [connection release]; 
} 

-(void) connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    if (connection == adCheckConnection) 
    { 
     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 

     //You've got all the data now 
     //Do something with your response string 


     [responseString release]; 
    } 

    [responseData release]; 
    [connection release]; 
} 
+0

這是不行的 – NouNou

+0

這將無法正常工作?你能多給我一點嗎? –

+0

thans James.try在我的文章中查看源代碼並查看腳本後的所有標記。它們在頁面加載時由腳本創建,我可以在Safari瀏覽器的開發工具中看到它們,但是當我下載html內容時,我可以在腳本後找不到所有。我很抱歉我的英語。 – NouNou

0

是源,只需使用視圖生成的源代碼在Firefox與Web開發人員擴展或擴展Firebug

+0

感謝,但我想下載生成的源和分析它們 – NouNou