0

How can I get an url of an image on any site programmatically?

Maybe I can do it with AFNetworking?

Thank you in advance!!!

Costa
  • 3
  • 3
  • Are you asking how to load an image from a URL you already have or are you asking how to get a URL? – rmaddy Nov 10 '12 at 19:00
  • I am asking how to get an url of every image on any site I input (for example I input the url of the site in the text field of my ios app) – Costa Nov 10 '12 at 19:02
  • You need to parse the HTML you get from the URL looking for tags or other image references. – rmaddy Nov 10 '12 at 19:04
  • Yes, I know! But how can I do it with AFNetworking? It is not a HTML parser...Maybe there is another method using Standart IOS SDK? – Costa Nov 10 '12 at 19:10
  • Lol! I haven't answered immediately due my being shocked xD – Costa Nov 10 '12 at 19:19
  • @Costa You really should update your question to be more clear. There is no way anyone would have known that your question really has to do with how to parse HTML to get a list of images. – rmaddy Nov 10 '12 at 19:19
  • Sorry, maybe I really asked it wrong, but it happend, as I think, due to the fact that I was asking such a question: "How to parse a HTML page to get list of images with AFNetworking?" everywhere and I was given the answers that AFNetworking is not for parsing HTML pages and when I asked this one, I could be answered in useful for me way! So sorry if I was wrong – Costa Nov 10 '12 at 19:24

1 Answers1

0

load the html, parse it using NSXMLParser and handle the callback for every img tag it finds. in there you can ask for the src attribute of that image tag and BOOM you get the url :)

also see this post for further info on how to use the parser Using an NSXMLParser to parse HTML

//MOCK-Code
NSXmlParser *p = [[NSXMLParser alloc] initWithURL:pageToParse];
p.delegate = self;
[p parse];

- parserDidStart:(id)nodeOfName withAttributes:(id)listOfAttributes {
    id src = [listOfAttributes objectForKey:@"src"];
    ...
Community
  • 1
  • 1
Daij-Djan
  • 49,552
  • 17
  • 113
  • 135
  • You realize that `NSXMLParser` is for parsing XML, not HTML. Very little HTML is valid XML. Did you read the link you referenced? The accepted answer talks about the issues of trying to parse HTML with `NSXMLParser`. – rmaddy Nov 10 '12 at 19:22
  • Yes, I realize it. Sure I read it. I think that I'd better to use HTMLParser for this...but is it true, that if I use NSXMLParser for parsing HTML on some sites I won't be able to get the images url? – Costa Nov 10 '12 at 19:30
  • @mdaddy I use it all the time btw :D if the html isnt valid xhtml I CTidy it. And If that is bad, I fallback to AQXMLParser (drop in replacement) – Daij-Djan Nov 10 '12 at 19:39
  • @Daij-Djan Will this work on a HTML, I just can't get it, because somewhere is written that it's OK and somewhere that it is bad ? Thank s – Costa Nov 10 '12 at 19:53
  • @costa yes its true.. without more config it fails to parse SOME html. You can drop down to libxml2 or KissXML or AQXMLParser. (https://github.com/zootreeves/Objective-C-HMTL-Parser also looks nice) OR you use CTidy before NSXMLParser.. that be the 'lightweight' way. – Daij-Djan Nov 10 '12 at 19:53
  • @Daij-Djan OK, thank you! and can I get the url in such a way http://stackoverflow.com/questions/6865258/nsxmlparser-rss-image-urls ? or maybe there is another better one? – Costa Nov 10 '12 at 19:55
  • no you get the callback that the parser found an image and of that you get the src, which IS the imageurl – Daij-Djan Nov 10 '12 at 20:02
  • id src = [listOfAttributes objectForKey:@"src"]; <------- Is it the image url? (sorry for dump questions) and thank you again) – Costa Nov 10 '12 at 20:06
  • yes :) that seems correct.. given that you use NSXMLParser or a comparable parser – Daij-Djan Nov 10 '12 at 20:17
  • OK)I'll try to include AFNetworking and HTMLParser together,because I also need to use Afnetworking) Danke =) – Costa Nov 10 '12 at 20:36