Objective-C & Cocoa Touch Issue involving Screen Scrapes in iOS
I am writing an iphone app that scrapes the URL www.drugs.com/imprints.php. It takes one of four parameters (name, imprint, color, shape), and plugs it into the a URL parameter which looks like:
http://www.drugs.com/imprints.php?action=search&imprint=%@&color=%@&shape=%@&drugname=%@
Here is a segment of code I've written to do the scraping:
NSString *name = [[NSString alloc] initWithFormat:@"%@", [ask text]];
NSString *imprint = [[NSString alloc] initWithFormat:@"%@", [imp text]];
NSString *color = [[NSString alloc] initWithFormat:@"%@", [col text]];
NSString *shape = [[NSString alloc] initWithFormat:@"%@", [sha text]];
NSString *address = @"http://www.drugs.com/imprints.php?action=search&drugname=";
NSString *request = [NSString stringWithFormat:@"http://www.drugs.com/imprints.php?action=search&impr开发者_如何学Cint=%@&color=%@&shape=%@&drugname=%@",imprint,color,shape,name];
NSString *escapedUrl = [request
stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *URL = [NSURL URLWithString:escapedUrl];
NSString *txtToScrape = [NSString stringWithContentsOfURL:URL encoding:NSASCIIStringEncoding error:&error];
NSString *drugName= [[[[txtToScrape componentsSeparatedByString:@"q="] objectAtIndex:1] componentsSeparatedByString:@">"] objectAtIndex:0];
NSString *drugImage = [[[[txtToScrape componentsSeparatedByString:@"images/pills/mmx/"] objectAtIndex:1] componentsSeparatedByString:@");"] objectAtIndex:0];
NSString *drugDesc = [[[[txtToScrape componentsSeparatedByString:@"/imprints/"] objectAtIndex:1] componentsSeparatedByString:@".html"] objectAtIndex:0];
Basically what the code does is return drug information back to the view, and puts it in strings. I am using componentsSeparatedByString, and am finding various "key" tags and words in the code, and am returning/parsing them based on those artifacts in the HTML.
Now my question is, is there a better way to do this? I am new to iOS programming, and feel like I am overlooking something that would be more efficient. Also if I were to distribute this in the AppStore, would it be legit since I am scraping a third party web site? I do not plan on selling it but giving it away for free. Lastly, I have an exit button in the UI, what is the legality of having an Exit Button in iOS Apps? Will Apple red flag it because of it? (the exit button does exit(0);).
Sorry, but I am new to Cocoa and iOS development, so apologies in advance for the questions if they are remedial.
If you check the TOS for www.drugs.com you'll likely find that they forbid unauthorized copies of any material found on their site. That being said, it's still a legal grey area. Companies like 80legs make a profit by crawling websites which forbid unauthorized reproduction and then selling the results. With that in mind, it's unlikely that you would face legal problems due to your scraping.
Based on other apps I've seen on the App Store I think it's pretty unlikely that Apple with notice or care either.
You have several questions bundled in one.
You can not have an exit button.
Quote from the HIG:
Don’t Quit Programmatically
Never quit an iOS application programmatically because people tend to interpret this as a crash. However, if external circumstances prevent your application from functioning as intended, you need to tell your users about the situation and explain what they can do about it. Depending on how severe the application malfunction is, you have two choices.
Display an attractive screen that describes the problem and suggests a correction. A screen provides feedback that reassures users that there’s nothing wrong with your application. It puts users in control, letting them decide whether they want to take corrective action and continue using your application or press the Home button and open a different application
If only some of your application's features are not working, display either a screen or an alert when people activate the feature. Display the alert only when people try to access the feature that isn’t functioning.
As far as parsing, you can use regular expressions in objective c, but what you had looked fine. For more information see Using regular expressions in iPhone SDK or take a look at RegexKitLite.
Lastly, I don't know the legality of screen scraping. :( Not a lawyer. I've seen good posts about it though.
https://stackoverflow.com/questions/822380/how-legal-is-screen-scraping
You should take a look at Wikipedia's entry on Web Scraping as well.
精彩评论