146

我需要从 UIWebView 中显示的 HTML 页面中提取标题标签的内容。这样做的最强大的方法是什么?

我知道我可以做到:

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

但是,这仅在启用 javascript 时才有效。

或者,我可以只扫描标题的 HTML 代码文本,但这感觉有点麻烦,并且如果页面的作者对他们的代码感到奇怪,可能会变得脆弱。如果涉及到这一点,在 iPhone API 中处理 html 文本的最佳方法是什么?

我觉得我忘记了一些明显的事情。有比这两种选择更好的方法吗?

更新:

从这个问题的答案开始:UIWebView: Can You Disable Javascript? 似乎没有办法在 UIWebView 中关闭 Javascript。因此,上面的 Javascript 方法将始终有效。

4

7 回答 7

88

对于那些只是向下滚动以找到答案的人:

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

这将始终有效,因为无法关闭 UIWebView 中的 Javascript。

于 2013-11-18T22:53:49.863 回答
6

WKWebView有'title'属性,就这样做,

func webView(_ wv: WKWebView, didFinish navigation: WKNavigation!) {
    title = wv.title
}

我觉得UIWebView现在不合适。

于 2016-12-06T04:41:13.943 回答
3

如果启用 Javascript 使用这个:-

NSString *theTitle=[webViewstringByEvaluatingJavaScriptFromString:@"document.title"];

如果禁用 Javascript 使用这个:-

NSString * htmlCode = [NSString stringWithContentsOfURL:[NSURL URLWithString:@"http://www.appcoda.com"] encoding:NSASCIIStringEncoding error:nil];
NSString * start = @"<title>";
NSRange range1 = [htmlCode rangeOfString:start];

NSString * end = @"</title>";
NSRange range2 = [htmlCode rangeOfString:end];

NSString * subString = [htmlCode substringWithRange:NSMakeRange(range1.location + 7, range2.location - range1.location - 7)];
NSLog(@"substring is %@",subString);

我在 NSMakeRange 中使用了 +7 和 -7 来消除<title>ie 7的长度

于 2015-06-02T05:28:09.843 回答
2

编辑:刚刚看到你找到了答案...... sheeeiiittt

我真的只是学到了这个!为此,您甚至不需要将其显示在 UIWebView 中。(但是当你使用它时,你可以获取当前页面的 URL)

无论如何,这是代码和一些(微弱的)解释:

    //create a URL which for the site you want to get the info from.. just replace google with whatever you want
    NSURL *currentURL = [NSURL URLWithString:@"http://www.google.com"];
    //for any exceptions/errors
    NSError *error;
    //converts the url html to a string
    NSString *htmlCode = [NSString stringWithContentsOfURL:currentURL encoding:NSASCIIStringEncoding error:&error];

所以我们有了 HTML 代码,现在我们如何获得标题?好吧,在每个基于 html 的文档中,标题都由 This Is the Title 发出信号 所以可能最简单的方法是在 htmlCode 字符串中搜索 , 和 for ,然后将其子串化,以便我们得到介于两者之间的内容。

    //so let's create two strings that are our starting and ending signs
    NSString *startPoint = @"<title>";
    NSString *endPoint = @"</title>";
    //now in substringing in obj-c they're mostly based off of ranges, so we need to make some ranges
    NSRange startRange = [htmlCode rangeOfString:startPoint];
    NSRange endRange = [htmlCode rangeOfString:endPoint];
    //so what this is doing is it is finding the location in the html code and turning it
    //into two ints: the location and the length of the string
    //once we have this, we can do the substringing!
    //so just for easiness, let's make another string to have the title in
    NSString *docTitle = [htmlString substringWithRange:NSMakeRange(startRange.location + startRange.length, endRange.location)];
    NSLog(@"%@", docTitle);
    //just to print it out and see it's right

就是这样!所以基本上要解释 docTitle 中发生的所有恶作剧,如果我们只是通过说 NSMakeRange(startRange.location, endRange.location) 来创建一个范围,我们将获得 startString 的标题和文本(即 ),因为位置是字符串的第一个字符。所以为了抵消它,我们只是添加了字符串的长度

现在请记住,此代码未经测试。如果有任何问题,可能是拼写错误,或者我没有/确实在不应该添加指针时添加了指针。

如果标题有点奇怪且不完全正确,请尝试使用 NSMakeRange ——我的意思是添加/减去字符串的不同长度/位置——任何看起来合乎逻辑的东西。

如果您有任何疑问或有任何问题,请随时提出。这是我在这个网站上的第一个答案,如果有点混乱,很抱歉

于 2013-11-21T06:32:28.167 回答
1

这是 Swift 4 版本,基于此处的答案

func webViewDidFinishLoad(_ webView: UIWebView) {
    let theTitle = webView.stringByEvaluatingJavaScript(from: "document.title")
}
于 2018-04-25T10:45:18.800 回答
0

到目前为止,我没有使用 webviews 的经验,但是,我相信它会将它的标题设置为页面标题,所以,我建议的一个技巧是在 webview 上使用一个类别并覆盖 self.title 的设置器,这样你就可以添加一条消息你们中的一个人反对或修改某些属性以获得标题。

你能试着告诉我它是否有效吗?

于 2013-11-20T23:57:38.623 回答
0

如果您在代码中经常需要它,我建议您像这样在“扩展 UIWebView”中添加一个 func

extension UIWebView {

    func title() -> String {
        let title: String = self.stringByEvaluatingJavaScript(from: "document.title")!
        return title
    }
}

或者最好使用 WKWebView。

不幸的是,它在 ARKit 中没有得到很好的支持。我不得不放弃 WKWebView。我无法将网站加载到 webView 中。如果有人在这里解决了这个问题->我有一个类似的问题,那将有很大帮助。

于 2019-07-14T19:12:00.483 回答