Today suddenly have a demand, the second page to display a page, the first page to display a picture title or introduction, etc., but! Backstage only returned a url! This means that I have to take the first page myself through the URL to get the content I need inside the page. Here is not detailed how to get title or pictures and so on, my demand is to get the text section of the article content to show three rows is enough:
① get the page content, very simple code
NSString *htmlstring = [NSString stringwithcontentsofurl:[nsurl urlwithstring:@ "Your full URL"] Encoding: Nsutf8stringencoding Error:nil];
② is removing the network label, a method
-(NSString *) getzzwithstring: (NSString *) string{
nsregularexpression *regularexpretion=[nsregularexpression regularexpressionwithpattern:@ "<[^>]*>|\n" options:0 Error:nil];
string = [regularexpretion stringbyreplacingmatchesinstring:string options:nsmatchingreportprogress Range: Nsmakerange (0, String.Length) withtemplate:@ ""];
return string;
}
③ Last
NSString *contentstr = [self getzzwithstring:htmlstring];
Contentstr is the text I want to use for display. This method gets the text is the URL corresponding to the text of the page, so according to the need to intercept the length of freedom.
Summary: The above is mainly in the case of not need to load the Web page to use, convenient and quick. If this page has webview, it can be through the JS code to obtain the corresponding content:
-(void) Webviewdidfinishload: (UIWebView *) webview {
UIWebView *web = webview;
Get all the HTML
nsstring *allhtml = @ "Document.documentElement.innerHTML";
Get page title
nsstring *htmltitle = @ "Document.title";
Gets a value for the page
nsstring *htmlnum = @ "document.getElementById (' title '). innertext";
Get the content of the Web page
nsstring *allhtmlinfo = [Web stringbyevaluatingjavascriptfromstring:allhtml];
NSLog (@ "%@", allhtmlinfo);
NSString *titlehtmlinfo = [Web Stringbyevaluatingjavascriptfromstring:htmltitle];
NSLog (@ "%@", titlehtmlinfo);
NSString *numhtmlinfo = [Web Stringbyevaluatingjavascriptfromstring:htmlnum];
NSLog (@ "%@", Numhtmlinfo);
}
Here's a quote about wandering in Jane's book