通过调用document.write创建解析表()

问题描述:

我想解析一个网页以获取该页面上的表格内容。但是,该表是通过调用document.write()和几个javascript函数创建的。即当我加载该页面的URL请求时,我得到的源代码不包含该表的html标记。是否有可能获得最终页面的源代码(即包含我想要的表格的html标签的源代码)?我非常好奇,我可以在Safari浏览器的开发工具中查看“最终页面源代码”,但不能查看页面的来源。 这是页面the source通过调用document.write创建解析表()

您应该使用实现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

是源,只需使用视图生成的源代码在Firefox与Web开发人员扩展或扩展Firebug

+0

感谢,但我想下载生成的源和分析它们 – NouNou

是源,只需使用视图生成的源代码在Firefox与Web开发人员扩展或扩展Firebug

+0

感谢,但我想下载生成的源和分析它们 – NouNou