简体   繁体   English

如何在iOS Swift上从网站(以HTML或JSON)检索文本和图像?

[英]How do I retrieve text and images from websites (in HTML or JSON) on iOS Swift?

I looked around the Internet to figure out how to get data from websites using SWIFT, and have narrowed it down to roughly JSON or HTML, which I am not familiar with. 我环顾互联网,以找出如何使用SWIFT从网站获取数据的方法,并将其范围缩小到大致不熟悉的JSON或HTML。

As far as I know, there are third party libraries for me to use to parse the data. 据我所知,我可以使用第三方库来解析数据。 I have been following Dani Arnaout's Working with JSON in Swift Tutorial as a reference. 我一直在遵循Dani Arnaout 在Swift Tutorial中的使用JSON作为参考。 However, I have not been able to find the way to retrieve the JSON from any random website. 但是,我还没有找到从任何随机网站检索JSON的方法。 Only the iTunes JSON page, which is included in the tutorial, works. 本教程中仅包含iTunes JSON页面。

What I want to do: Make an app that downloads images and also some text from many different websites, either by HTML or JSON. 我要做什么:制作一个应用程序,它可以通过HTML或JSON从许多不同的网站下载图像以及一些文本。 The problem right now is that I have no idea how to start doing it. 现在的问题是我不知道如何开始。 A simple demo would be helpful. 一个简单的演示会有所帮助。

Questions 问题

  1. How do I get the JSON of any random website on the Internet? 如何获取Internet上任何随机网站的JSON?

  2. How do I make use of the HTML data from websites to turn it into a readable format? 如何利用网站的HTML数据将其转换为可读格式? I'm retrieving the HTML using Google Chrome, and it seems to be gibberish: I can't find the text anywhere. 我正在使用Google Chrome检索HTML,但似乎有点乱:我在任何地方都找不到文本。

Question 1 : Not all websites expose their content formatted as JSON. 问题1 :并非所有网站都公开其格式为JSON的内容。

Question 2 : I think you should look at a couple of resources. 问题2 :我认为您应该查看一些资源。 First Ray Wenderlich has a tutorial on how to parse HTML . First Ray Wenderlich有一个有关如何解析HTML的教程 Although it is using Objective-C you should be able to learn quite a lot there. 尽管它使用的是Objective-C,但您应该可以在那里学到很多东西。

When you have read that tutorial I would recommend you look at the Swift library Alamofire . 阅读该教程后,建议您看一下Swift库Alamofire There is another tutorial on the Wenderlich site covering this library. Wenderlich网站上还有另一本教程涵盖该库。

Happy coding! 编码愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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