在IOS上使用SwiftSoup解析元素

时间:2018-06-05 18:51:16

标签: ios parsing html-parsing elements swiftsoup

我需要帮助将html中的某个标记转换为数组,我可以获取标题标记,但无法获取“feedburner:origEnclosureLink”标记。谁能指出我正确的方向,我做错了什么?  这是我尝试过的代码。

func setUpTable(){
    if let url = URL(string:     "http://feeds.feedburner.com/TheWestHamWayOnPhoenixFm") {
        do {
            contents = try String(contentsOf: url, encoding: .utf8)
            print(contents)

            do{

                let doc = try SwiftSoup.parse(contents)

                do{


                    let link = try! doc.select("item").array()
                     // print(link.count)
                     //  print(titleString)
                    for pods in link{

                        let each = try pods.select("title").array()
                        for theTitle in each {
                            let titleText = try theTitle.text()
                            titles.append(titleText)
                            print(titleText)
                        }
let links = try pods.select("feedburner:origEnclosureLink").array()
                        for theLinks in links {
                            let streamIP = try theLinks.text()
                            ipAdds.append(streamIP)
                            print(streamIP)
                        }
                     }
                }catch{
                    // error getting element
                }

            }catch{
                // error parse html string
            }

        } catch {
            // contents could not be loaded
        }
    } else {
        // the URL was bad!
    }
    DispatchQueue.main.async {
        self.tableView.reloadData()
    }
}

这是我要解析的HTML的片段

<item><title>The West Ham Way show 67 - Wed 20 Dec 2017</title><link>http://feedproxy.google.com/~r/TheWestHamWayOnPhoenixFm/~3/oX2Ab8EAJe0/the-west-ham-way-show-67-wed-20-dec-2017.html</link><category>podcast</category><author>paul@phoenixfm.com (Phoenix FM)</author><pubDate>Wed, 20 Dec 2017 12:31:30 PST</pubDate><guid isPermaLink=\"false\">tag:blogger.com,1999:blog-7710988231001933395.post-6260568670904882508</guid><description>&lt;img src=\"http://feeds.feedburner.com/~r/TheWestHamWayOnPhoenixFm/~4/oX2Ab8EAJe0\" height=\"1\" width=\"1\" alt=\"\"/&gt;</description><thr:total xmlns:thr=\"http://purl.org/syndication/thread/1.0\">0</thr:total><media:content url=\"http://feedproxy.google.com/~r/TheWestHamWayOnPhoenixFm/~5/UGwuCMSsw1s/whw67.mp3\" type=\"audio/mpeg\" /><itunes:explicit>no</itunes:explicit><itunes:author>Phoenix FM</itunes:author><itunes:keywords>WestHam,west,ham,west,ham,united,whufc,WHUFC,irons,hammers,claret,blue,football,soccer,EPL,exwhuemployee,sex,drugs,carlton,cole</itunes:keywords><feedburner:origLink>http://thewesthamwayonphoenixfm.blogspot.com/2017/12/the-west-ham-way-show-67-wed-20-dec-2017.html</feedburner:origLink><enclosure url=\"http://feedproxy.google.com/~r/TheWestHamWayOnPhoenixFm/~5/UGwuCMSsw1s/whw67.mp3\" length=\"0\" type=\"audio/mpeg\" /><feedburner:origEnclosureLink>http://www.phoenixfm.com/whw/whw67.mp3</feedburner:origEnclosureLink></item>

谢谢。

0 个答案:

没有答案