UIWebView-如何从Facebook等网页中提取HTML代码

UIWebView - how to extract the HTML code from webpages like Facebook?

本文关键字:提取 HTML 代码 网页 Facebook UIWebView-      更新时间:2023-09-26

我有一个源代码的例子,它用程序设置HTML中字段的值。

源代码的部分如下:

- (void)viewDidLoad
{
[super viewDidLoad];
myWebView.delegate = self;
//---formulate the HTML string---
NSString *str = [[[NSString alloc] initWithString:
                @"<html>"
                 "    <body>"
                 "        <h1>The fields below are filled in programmatically</h1>"
                 "        <input id='username' value='UserName' />"
                 "        <input id='password' value='Password' type='password' />"
                 "    </body>"
                 "</html>"
                 ] autorelease];
//---load the UIWebView with the html string---
[myWebView loadHTMLString:str baseURL:nil];
}
//---wait for the UIWebView to finish loading---
- (void)webViewDidFinishLoad:(UIWebView *)webView {
//---access the 2 fields in the HTML---
[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('username').value='Wei-Meng Lee';"];
[myWebView stringByEvaluatingJavaScriptFromString:@"document.getElementById('password').value='TopSecret';"];
}

我想通过编程设置像Facebook这样的网页中的字段值,而不是像上面写的那样的自定义HTML。

我应该如何将"str"替换为实际网页的html源代码,以便选择"ElementbyId"?

通过替换

//---formulate the HTML string---
NSString *str = [[[NSString alloc] initWithString:
            @"<html>"
             "    <body>"
             "        <h1>The fields below are filled in programmatically</h1>"
             "        <input id='username' value='UserName' />"
             "        <input id='password' value='Password' type='password' />"
             "    </body>"
             "</html>"
             ] autorelease];
//---load the UIWebView with the html string---
[myWebView loadHTMLString:str baseURL:nil];

使用[UIWebView loadRequest],它将加载带有URL的web视图,UIWebView将包含使stringByEvaluatingJavaScriptFromString工作的HTML。