簡體   English   中英

UIWebView-如何從Facebook之類的網頁中提取HTML代碼?

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

我有一個源代碼示例,該源代碼以編程方式設置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將包含HTML以使stringByEvaluatingJavaScriptFromString起作用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM