views:

685

answers:

6

Is there a way to parse a website's source on the iPhone to get the URL's of photos on that page? If so how would you do that?

Thanks

+1  A: 

You could try it using regular expressions, but I wouldn't recommend that. You should have a look at NSXMLParser, assuming the webpage is coded to be XHTML compliant. TouchXML is another good library.

Dan Lorenc
+2  A: 

There is no super easy way. When I had to do it I wrote a libxml2 SAX parser. libxml2 has an html reader that works fairly well with malformed html, and libxml2 is included with the base system.

Louis Gerbarg
A: 

take a look at Event Driven XML Parsing in the iPhone reference library

ctshryock
A: 

Are you OK with any approach you use not picking up on images loaded dynamically via javascript.

The closest thing I could see working is to parse out any javacript imports, load those up too, and then use a regular expression across the whole file looking for anything that ends in ".jpg/.gif/.png" and grab the full URL out from that. The libxml approach would miss out on references to images not in img tags... But it might well be good enough.

Kendall Helmstetter Gelner
+3  A: 

I'd say go for regular expressions - there is a one page library that wraps c regexesthat you can drop into your project.

Agree, you don't need to parse the entire doc just to get the <img> tags.
Marco Mustapic
+2  A: 

I recommend regular expressions. There's a great open source Regex library for Cocoa called RegexKit. For the most part, you can just drop it in your code and it'll "just work".

Getting all the urls of images wouldn't be too difficult (less than 20 lines of code) if you assume that all images are going to be in <img> tags. You'd just grab all the image tags (something like: <img\s+[^>]+>), then iterate through those matches. For each match, you'd pull out whatever's in the src attribute: src\s*=\s*("|')?\s*([^\s"']+)(\s|"|')

You might need to tweak that a bit, but it shouldn't be too bad.

Dave DeLong