Extracting Text from url Xcode? - objective-c

I am new to Objective C.
I was trying to extract text from a webpage and display it in a textView;
Except for when I run the app it appears to show html instead of the article.
NSURL *URL = [NSURL URLWithString:[self.url
stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]];
NSData *theData = [NSData dataWithContentsOfURL:URL];
NSString *content = [[NSString alloc] initWithData:theData encoding:NSStringEncodingConversionAllowLossy];
_viewPage.text = content;
The viewPage is the textview itself. How do I extract the text only?

You have not included any code to extract the text from the web page. When you run "dataWithContentsOfURL" and it's a web page, then, as you have seen, you get the whole page.
To extract the data you need to process the results. In a simple case you can get the text with some string manipulation. In more complex cases you should look for a library which will parse the whole page for you. You can then access the parsed structure to get the content that you want.
Look here for an example of the simple case;
http://natashatherobot.com/html-css-parser-objective-c/
There are libraries for the more complex case.

Related

Google image from cocoa

I'd like to write a program in cocoa that parse a google image webpage and extract the images.
i use a code like this:
NSURL *url = [ NSURL URLWithString: [ NSString stringWithFormat: #"https://www.google.it/search?q=%#&tbm=isch", searchString] ];
NSStringEncoding enc;
NSString *test = [NSString stringWithContentsOfURL:url usedEncoding:&enc error:NULL];
The problem is that the page that is returned in this way is different from what it is in a browser.
I'don't get the imgurl parameter with the url of the full image. only the thumbnails.
There is a way to have the complete google images results in cocoa like i have in firefox?
Thank you
What you are doing is not correct way.
To get list of images, you should use Google API for image Search
Follow below link for more details.
https://developers.google.com/image-search/v1/jsondevguide
The URL for webservice would look like below.
https://ajax.googleapis.com/ajax/services/search/images?q=soccer&v=1.0
^^^^^^ your search keyword here...

Get Information About a Website Using there URL in Xcode

Im trying to Get Information About a Website like title (Like what you see in safari in the Tabs and Windows)and Description of that Website from its URL Without Using the WebKit Framework in Xcode and Displaying it on screen as text. I Just want to know have to obtain the title of the Website/Page that the URL Belongs to using Cocoa. Is There a Tutorial or Something. It will be a Big Help.
Try this:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString* title = [webView stringByEvaluatingJavaScriptFromString: #"document.title"];
navbar.title = title;
}
Taken from: http://blog.mcohen.me/2008/12/12/getting-the-title-of-a-web-view-in-cocoa-touch/
If you don't want to display the webview, make it hidden and use above delegate method to get what you want...
A way you could do it is by downloading the HTML content at the target url and parse it, for example using hpple.
Example (not tested):
NSURL *url = [NSURL URLWithString:#"http://..."];
//Only using this for simplicity of the example.
//You should really use an asynchronous method instead (see `NSURLSession`).
NSData *urlData = [NSData dataWithContentsOfURL:url];
//Parse HTML and search for title element
TFHpple *hppleParser = [[TFHpple alloc] initWithHTMLData:data];
TFHppleElement *titleElement = [hppleParser search:#"//title"][0];
NSString titleString = titleElement.text;

Unable to retrieve certain pages using stringWithContentsOfURL

I am trying to get HTML files from the web, using stringWithContentsOfURL:. My problem is, sometimes it works but sometimes it doesn't. For example, I tried:
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
encoding:encoding1
error:nil];
NSLog(#"html = %#",string);
This works fine, but when I replace the URL with #"http://www.youtube.com/" then I only get "NULL". Is there anyone that knows what's going on? Is it because of YouTube having some sort of protection?
Google's home page uses ISO-8859-1 encoding (aka "Latin-1", or NSISOLatin1StringEncoding). YouTube uses UTF-8 (NSUTF8StringEncoding), and the encoding you've specified with your encoding1 variable has to match the web page in question.
If you just want the web page and don't really care what encoding it's in, try this:
NSStringEncoding encoding;
NSError *error;
NSString *string = [NSString stringWithContentsOfURL:
[NSURL URLWithString:#"http://www.google.com/"]
usedEncoding:&encoding
error:&error];
NSLog(#"html = %#",string);
This method will tell you what the encoding was (by writing it to the encoding variable), but you can just throw that away and focus on the string.

Parse XML and generate UI in UIWebView iPad App

I will receive an XML from Server, and have to parse the xml file, and based on that have to populate textbox, listbox etc(a form) in UIWebView.
I know XML parsing for iOS using NSXML, I do not know how to fire events to generate Form/UI at runtime.
Any tutorials or idea pls.
If you have created your HTML as a NSString already, you can load it into the UIWebView by using the method loadHTMLString:baseURL:. For example:
NSString *htmlString = [[NSString alloc] init];
//Download XML, parse it and turn it into HTML.
[myWebView loadHTMLString:htmlString baseURL:nil];
//if you have to do any thing else to the HTML string do it here
[htmlString release];
Changing the htmlString after calling loadHTMLString:baseURL: won't update the UIWebView. You will need to call the method again, sending the modified string.
If you need further help, comment below and I'll edit my answer or comment back.

How do i grab numbers from a Table on a website in my Cocoa App?

Ok this is a more specific version of my last question.
So on a website there exists some data that is coded in HTML into a table.
In my Cocoa app, I want to download the html code of the website and then read through the html and snag the data from it. I was hoping someone could point out some useful classes/methods for accomplishing the retrieval of the website and putting it into some format where I can read through the code in my program?
Thanks in advance!
Try using hpple, it's an HTML parser for ObjC.
here's an example using it:
#import "TFHpple.h"
NSData *data = [[NSData alloc] initWithContentsOfFile:#"example.html"];
// Create parser
xpathParser = [[TFHpple alloc] initWithHTMLData:data];
//Get all the cells of the 2nd row of the 3rd table
NSArray *elements = [xpathParser search:#"//table[3]/tr[2]/td"];
// Access the first cell
TFHppleElement *element = [elements objectAtIndex:0];
// Get the text within the cell tag
NSString *content = [element content];
[xpathParser release];
[data release];