xcode 如何从网站中提取数据并将其放在应用程序上?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7424700/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I pull data from a website and put it on an app?
提问by Craig
I'm new to iPhone app development. I was thinking about making a simple app, just for learning purposes. For example, I want to show the scores of sports games (NBA, NFL, NHL, etc.) What would be the easiest way for me to pull that data from, say, http://www.espn.comand display it on my app?
我是 iPhone 应用程序开发的新手。我正在考虑制作一个简单的应用程序,仅用于学习目的。例如,我想显示体育比赛(NBA、NFL、NHL 等)的比分。对我来说,从http://www.espn.com提取数据并显示它的最简单方法是什么?在我的应用程序上?
Thanks!
谢谢!
回答by Fernando Cervantes
First of all, you want to find an RSS Feed, you can find some of ESPN links here, once you have an RSS URL, all you have to do is type it between the quotes in the following string.
首先,你想找到一个 RSS 提要,你可以在这里找到一些 ESPN 链接,一旦你有了一个 RSS URL,你所要做的就是在以下字符串的引号之间输入它。
-(void) viewDidLoad {
NSString * sFeedURL = [NSString stringWithFormat:@"http://www.google.com/ig/api?weather=,,,270000,960000"];
//RSS Feed URL goes between quotes
NSString * sActualFeed = [NSString stringWithContentsOfURL:[NSURL URLWithString:sFeedURL] encoding:1 error:nil];
lblOne.text = sActualFeed;
NSLog(@"%@", sActualFeed);
}
The URL provided is used to get the current weather, which is not what you want, but all you have to do is change that to the RSS Feed URL you want to display.
提供的 URL 用于获取当前天气,这不是您想要的,但您所要做的就是将其更改为要显示的 RSS Feed URL。
The Feed will show up in a Label. Of course you have to make it first, I called it lblOne, but you don't need to have a label, you can use NSLog too just to test it out.
Feed 将显示在标签中。当然你得先做出来,我叫它lblOne,但是你不需要有标签,你也可以用NSLog来测试一下。
回答by Ben Baron
The cleanest way would be to find data sources that are provided in XML or JSON format, then use the XML or JSON parsing library of your choice to parse and use the data.
最简洁的方法是查找以 XML 或 JSON 格式提供的数据源,然后使用您选择的 XML 或 JSON 解析库来解析和使用数据。
If you can't find any data sources like that, you can use an HTML scraping library (or be extra hacky about it and load a UIWebView offscreen and traverse the DOM) but you will most likely have constant problems with it as any time the website changes it can completely break your app. Also parsing HTML usually requires downloading a lot more data than XML or JSON files.
如果你找不到任何这样的数据源,你可以使用一个 HTML 抓取库(或者对它更加hacky 并在屏幕外加载一个 UIWebView 并遍历 DOM)但是你很可能会在任何时候遇到它的持续问题网站更改可能会完全破坏您的应用程序。此外,解析 HTML 通常需要下载比 XML 或 JSON 文件更多的数据。
回答by Calvin Cheng
Using Objective C, you can parse an RSS feed or call an API on a remote site (if you have access to that API) which returns a JSON/XML to your app.
使用目标 C,您可以解析 RSS 提要或调用远程站点上的 API(如果您有权访问该 API),它会向您的应用程序返回 JSON/XML。
There's a similar answer to such a question here - What's the best way to parse RSS/Atom feeds for an iPhone application?
这里有对这样一个问题的类似答案 -解析 iPhone 应用程序的 RSS/Atom 提要的最佳方法是什么?