0

Web からいくつかのコンテンツを取得するために、このタスクを作成しました。

let task = NSURLSession.sharedSession().dataTaskWithURL(url!, completionHandler: { (data, response, error) -> Void in

            if error == nil {

                var urlContent = NSString(data: data, encoding: NSUTF8StringEncoding) as NSString!

                // Get the part that you are interested in from the web page

                var urlContentArray = urlContent.componentsSeparatedByString("<div class=\"entry-summary\" itemprop=\"text\">")

                // Check if the array contains a value before you print

                if urlContentArray.count > 0 {

                    var newsArray = urlContentArray[1].componentsSeparatedByString("</div>")

                    var news = newsArray[0] as String

                    self.webView.loadHTMLString(news, baseURL: nil)

                    self.webContentLabel.text = news

                }

            } else {

                println("Error")

            }

        })

私の問題は、最初に取得している文字列と<p>興味のあるテキストを含む の間に多くのスペースがあるように Web サイトがフォーマットされていることです。したがって、取得した<p>テキストの先頭に を取得します。<p>取得している文字列の最後にを追加すると、配列が空であるというエラーが表示されます。この理由は、実際のhtmlファイルの「 ...itemprop="text">」と「 」の間のスペースだと思いますか?<p>

何か案は?

4

0 に答える 0