IOS gets Web page text content via URL __ios

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: