I need to extract the contents of the title tag from an HTML page displayed in a UIWebView. What is the most robust means of doing so?
我需要从UIWebView中显示的HTML页面中提取标题标签的内容。最有力的方法是什么?
I know I can do:
我知道我能做到:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
However, that only works if javascript is enabled.
但是,只有在启用javascript时才会这样做。
Alternatively, I could just scan the text of the HTML code for the title but that feels a bit cumbersome and might prove fragile if the page's authors got freaky with their code. If it comes to that, what's the best method to use for processing the html text within the iPhone API?
或者,我可以只扫描标题的HTML代码的文本,但这感觉有点麻烦,如果页面的作者对他们的代码感到奇怪的话,这可能会被证明是脆弱的。如果是这样,在iPhone API中处理html文本的最佳方法是什么?
I feel that I've forgotten something obvious. Is there a better method than these two choices?
我觉得我忘记了一些显而易见的事情。有比这两个选择更好的方法吗?
Update:
Following from the answer to this question: UIWebView: Can You Disable Javascript? there appears to be no way to turn off Javascript in UIWebView. Therefore the Javascript method above will always work.
从这个问题的答案:UIWebView:你能禁用Javascript吗?似乎没有办法关闭UIWebView中的Javascript。因此,上面的Javascript方法将始终有效。
6 个解决方案
#1
84
For those who just scroll down to find the answer:
对于那些只是向下滚动才能找到答案的人:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
This will always work as there is no way to turn off Javascript in UIWebView.
这将始终有效,因为在UIWebView中没有关闭Javascript的方法。
#2
3
If Javascript Enabled Use this :-
如果启用Javascript,请使用:-
NSString *theTitle=[webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
If Javascript Disabled Use this :-
如果禁用了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);
I Used +7 and -7 in NSMakeRange to eliminate the length of <title>
i.e 7
我在NSMakeRange中使用+7和-7消除了
#3
2
WKWebView
has 'title' property, just do it like this,
WKWebView有" title "属性,像这样,
func webView(_ wv: WKWebView, didFinish navigation: WKNavigation!) {
title = wv.title
}
I don't think UIWebView
is suitable right now.
我认为UIWebView现在不适合。
#4
1
Edit: just saw you found out the answer... sheeeiiitttt
编辑:刚看到你找到答案……sheeeiiitttt
I literally just learned this! To do this, you don't even need to have it displayed in UIWebView. (But as you are using it, you can just get the URL of the current page)
我真的学到了这个!要做到这一点,您甚至不需要在UIWebView中显示它。(但当您使用它时,您可以获得当前页面的URL)
Anyways, here's the code and some (feeble) explanation:
不管怎样,这是代码和一些(无力的)解释:
//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];
So we have the HTML code, now how do we get the title? Well, in every html-based doc the title is signaled by This Is the Title So probably the easiest thing to do is to search that htmlCode string for , and for , and substring it so we get the stuff in between.
我们有了HTML代码,我们怎么得到标题?在每一个基于html的文档中标题都是这样的标题所以可能最简单的方法就是搜索htmlCode字符串for, for, substring以便我们在两者之间找到东西。
//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
And that's really it! So basically to explain all the shenanigans going on in the docTitle, if we made a range just by saying NSMakeRange(startRange.location, endRange.location) we would get the title AND the text of startString (which is ) because the location is by the first character of the string. So in order to offset that, we just added the length of the string
这就是它!所以基本上是为了解释docTitle中所有的诡计,如果我们只说NSMakeRange(startRange)我们将获得startString(也就是)的标题和文本,因为位置是由字符串的第一个字符决定的。为了抵消这个,我们只增加了弦的长度
Now keep in mind this code is not tested.. if there are any problems it might be a spelling error, or that I didn't/did add a pointer when i wasn't supposed to.
请记住这段代码没有经过测试。如果有任何问题,可能是拼写错误,或者我没有在不应该添加指针的时候添加指针。
If the title is a little weird and not completely right, try messing around with the NSMakeRange-- I mean like add/subtract different lengths/locations of the strings --- anything that seems logical.
如果标题有点奇怪,而且不完全正确,可以尝试使用NSMakeRange——我的意思是添加/减去字符串的不同长度/位置——任何看起来合乎逻辑的东西。
If you have any questions or there are any problems, feel free to ask. This my first answer on this website so sorry if it's a little disorganized
如果您有任何问题或有任何问题,请随时提问。这是我在这个网站上的第一个答案,如果有点混乱的话,很抱歉
#5
0
I dońt have experience with webviews so far but, i believe it sets it´s title to the page title, so, a trick I suggest is to use a category on webview and overwrite the setter for self.title so you add a message to one of you object or modify some property to get the title.
我dońt webview迄今为止的经验,但我相信它集´s标题页面标题,所以,欺骗我建议是使用webview类别和覆盖的setter自我。因此,您可以向其中一个对象添加一条消息,或者修改一些属性以获得标题。
Could you try and tell me if it works?
你能不能试着告诉我它是否有效?
#6
0
Here is Swift 4 version, based on answer at here
以下是Swift 4版本,基于这里的回答
func webViewDidFinishLoad(_ webView: UIWebView) {
let theTitle = webView.stringByEvaluatingJavaScript(from: "document.title")
}
#1
84
For those who just scroll down to find the answer:
对于那些只是向下滚动才能找到答案的人:
- (void)webViewDidFinishLoad:(UIWebView *)webView{
NSString *theTitle=[webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}
This will always work as there is no way to turn off Javascript in UIWebView.
这将始终有效,因为在UIWebView中没有关闭Javascript的方法。
#2
3
If Javascript Enabled Use this :-
如果启用Javascript,请使用:-
NSString *theTitle=[webViewstringByEvaluatingJavaScriptFromString:@"document.title"];
If Javascript Disabled Use this :-
如果禁用了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);
I Used +7 and -7 in NSMakeRange to eliminate the length of <title>
i.e 7
我在NSMakeRange中使用+7和-7消除了
#3
2
WKWebView
has 'title' property, just do it like this,
WKWebView有" title "属性,像这样,
func webView(_ wv: WKWebView, didFinish navigation: WKNavigation!) {
title = wv.title
}
I don't think UIWebView
is suitable right now.
我认为UIWebView现在不适合。
#4
1
Edit: just saw you found out the answer... sheeeiiitttt
编辑:刚看到你找到答案……sheeeiiitttt
I literally just learned this! To do this, you don't even need to have it displayed in UIWebView. (But as you are using it, you can just get the URL of the current page)
我真的学到了这个!要做到这一点,您甚至不需要在UIWebView中显示它。(但当您使用它时,您可以获得当前页面的URL)
Anyways, here's the code and some (feeble) explanation:
不管怎样,这是代码和一些(无力的)解释:
//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];
So we have the HTML code, now how do we get the title? Well, in every html-based doc the title is signaled by This Is the Title So probably the easiest thing to do is to search that htmlCode string for , and for , and substring it so we get the stuff in between.
我们有了HTML代码,我们怎么得到标题?在每一个基于html的文档中标题都是这样的标题所以可能最简单的方法就是搜索htmlCode字符串for, for, substring以便我们在两者之间找到东西。
//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
And that's really it! So basically to explain all the shenanigans going on in the docTitle, if we made a range just by saying NSMakeRange(startRange.location, endRange.location) we would get the title AND the text of startString (which is ) because the location is by the first character of the string. So in order to offset that, we just added the length of the string
这就是它!所以基本上是为了解释docTitle中所有的诡计,如果我们只说NSMakeRange(startRange)我们将获得startString(也就是)的标题和文本,因为位置是由字符串的第一个字符决定的。为了抵消这个,我们只增加了弦的长度
Now keep in mind this code is not tested.. if there are any problems it might be a spelling error, or that I didn't/did add a pointer when i wasn't supposed to.
请记住这段代码没有经过测试。如果有任何问题,可能是拼写错误,或者我没有在不应该添加指针的时候添加指针。
If the title is a little weird and not completely right, try messing around with the NSMakeRange-- I mean like add/subtract different lengths/locations of the strings --- anything that seems logical.
如果标题有点奇怪,而且不完全正确,可以尝试使用NSMakeRange——我的意思是添加/减去字符串的不同长度/位置——任何看起来合乎逻辑的东西。
If you have any questions or there are any problems, feel free to ask. This my first answer on this website so sorry if it's a little disorganized
如果您有任何问题或有任何问题,请随时提问。这是我在这个网站上的第一个答案,如果有点混乱的话,很抱歉
#5
0
I dońt have experience with webviews so far but, i believe it sets it´s title to the page title, so, a trick I suggest is to use a category on webview and overwrite the setter for self.title so you add a message to one of you object or modify some property to get the title.
我dońt webview迄今为止的经验,但我相信它集´s标题页面标题,所以,欺骗我建议是使用webview类别和覆盖的setter自我。因此,您可以向其中一个对象添加一条消息,或者修改一些属性以获得标题。
Could you try and tell me if it works?
你能不能试着告诉我它是否有效?
#6
0
Here is Swift 4 version, based on answer at here
以下是Swift 4版本,基于这里的回答
func webViewDidFinishLoad(_ webView: UIWebView) {
let theTitle = webView.stringByEvaluatingJavaScript(from: "document.title")
}