简体   繁体   中英

'Pull' Images and Specific Text from a Website using Objective-C

Exactly what the name suggests. I have looked at other versions of this question and they do not exactly do what I need them to do.

My goal is to pull data from a website. Let's say, for example's sake, this pet adoption website . From here, the app will be able to specify whether you want a cat, dog, guinea pig, bird, dragon, whatever. Then, it would bring you to a ViewController that will display:

  1. The animal's picture.
  2. The animal's name.
  3. The animal's 'About Me'.
  4. And finally, the animal's price.

Of course, I will be able to display all the information, but I am not very adept at online capabilities.

How can I retrieve the above information and store it in such a way that I can use it to make a view of the animal?

you can only interface with a web view via javascript. you can only transfer 'string data' .. so for getting pictures, the approach has to be: convert o base64 encoded data on JS side, transfer it and use it in objC then to reconstruct a UIImage.


to not reinvent the wheel I'd like propose my own project (Disclaimer: Own code):

It offers an easy-to-use Javascript-ObjC bridge for IOS and OSX - partly based on the ideas of the JSBridge project

https://github.com/Daij-Djan/DDEnhancedJSBridge

This is a answer quite late but I think it still might help in your future. You can go into parsing the website and that is the right way to do it but I will show you how to do it a different way, this can also be used to read xml, html, .com, anything and also, .rss so it can read RSS Feeds.
Here :

This can get your first paragraph, if you request I will show you how to get the second paragraph and so on. So if you want to get money change your scanner to find $ Title would be name but if it is not tell me I will show you how to get second title or header. See this and down. I will show you how to get the html or xml from the whole website so you can see the whole code of the website and look for the about so read the website first and then read the code and you see the dogs name in the code find what it is under title tag or under header and change the code underneath to your needs read this whole post to understand everything. This below is main code of how to get info. Go to second code block to find out how to get code of your website in your Log or Console. To find your image same thing look through the NSLog and look for any image links like www.example.com/images/image.jpg or image.png or image.gif or some image extension and look at what code is initiating this like is it or there are literally 100's of image codes in html and xml, look for the url look at the code and then use it in your scanner change the

to your code and then make sure only the url of the image is still there like keep shortening it until you have nothing except url.

So you have

you want : www.example.com/images/image.jpg

You would use the scanner below and change the

to <media:content url:\\" so scan it until it is static so if you know that that part will always be the same then scan it up to there and then the second part of the scanner end it with \\"

See the first code block underneath the big one? This is the part we need to change -

NSString *webString2222 = mastaString;

        NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];



        NSString *content2222 = [[NSString alloc] init];




        //Change <p> to suit your need like <description> or <h1>
        [stringScanner2222 scanUpToString:@"<p>" intoString:Nil];



        [stringScanner2222 scanUpToString:@"." intoString:&content2222];







        NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"<p>" withString:@""];

        description.text = filteredTitle;

In our case we change this part to -

[stringScanner2222 scanUpToString:@"<media:content url:\"" intoString:Nil];
                [stringScanner2222 scanUpToString:@"\">" intoString:&content2222];

Now it has the url but the first part of the scanner always stays so use -

NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"

Then use this to download the image -

NSURL * imageURL = [NSURL URLWithString:[filteredTitle stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation([UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]])];

        UIImage * image = [UIImage imageWithData:imageData];

        [imageView setImage:image];

//This is your URL NSURL *URL = [NSURL URLWithString:@"URL HERE"]; //This is the data your pulling (dont change) NSData *data = [NSData dataWithContentsOfURL:URL];

// Assuming data is in UTF8. (dont change)
NSString *string = [NSString stringWithUTF8String:[data bytes]];
//Your textView your not done.
description.text = string;



//Do this with your textview

NSString *webStringz = description.text;



// Leave this
NSString *mastaString;
mastaString = webStringz;
{
    NSString *webString2222 = mastaString;



    NSScanner *stringScanner2222 = [NSScanner scannerWithString:webString2222];



    NSString *content2222 = [[NSString alloc] init];




    //Change <p> to suit your need like <description> or <h1>
    [stringScanner2222 scanUpToString:@"<p>" intoString:Nil];



    [stringScanner2222 scanUpToString:@"." intoString:&content2222];







    NSString *filteredTitle = [content2222 stringByReplacingOccurrencesOfString:@"<p>" withString:@""];

    description.text = filteredTitle;


}

Title ? Same deal change the <p> to a <title> in RSS <description> and <title> . Image ? Same deal change the <p> to what ever your RSS or website uses to get a image to find But remember for both of them when you change the

` you see the link which says stringByReplacingOccurences of you have to change that as well.

out then you have to delete this and make your code like this :

//This is your URL
        NSURL *URL = [NSURL URLWithString:@"URL HERE"];
        //This is the data your pulling (dont change)
        NSData *data = [NSData dataWithContentsOfURL:URL];

        // Assuming data is in UTF8. (dont change)
        NSString *string = [NSString stringWithUTF8String:[data bytes]];
        //Your textView your not done.
        description.text = string;
        NLog(@"%@", string)


        //Do this with your textview

        NSString *webStringz = description.text;



        // Leave this
        NSString *mastaString;
        mastaString = webStringz;

Now check your log it shows your whole website html or rss code then you scroll and read it and find your image link and check the code before it and change the String Scanner to your needs which is quite awesome and you have to change the stringByReplacingOccurences of.

Like I said images are a bit tricky when you do it with this method but XML Parsing is a lot easier ONCE you learn it , lol. If you request I will show you how to do it. Tell me if your having problems looking through the html or xml and specify which website again and I will look through and I will tell you how to get everything you want. Make sure :

If you want me to show you how to do it in XML just comment. If you want me to show you how to find the second paragraph or image or title or something just comment.

IF YOU NEED ANYTHING JUST COMMENT.

Bye have fun with code I provided, anything wrong JUST COMMENT! !!!!

:D

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM