我正在移动博客上工作我必须解析来自服务器的html数据,并按正确的顺序分别显示数据。我收到了这个回复。
<p>Travel to the 7 wonders of the world is an impressive undertaking! Some people have waited their whole lives to cross these destinations off their dream list. So that you can start your planning your packing list accordingly, here’s a list of the 7 wonders of the world and their locations:</p>\n\n<ol>\n\t<li><a href=\"http://www.ancient.eu/Chichen_Itza/\">Chichen Itza, Mexico</a></li>\n\t<li><a href=\"http://www.history.com/topics/great-wall-of-china\">Great Wall of China</a></li>\n\t<li><a href=\"http://www.machupicchu.org/\">Machu Picchu, Peru</a></li>\n\t<li><a href=\"https://www.britannica.com/topic/Christ-the-Redeemer\">Christ the Redeemer Statue, Rio de Janeiro</a></li>\n\t<li><a href=\"http://www.history.com/topics/ancient-history/colosseum\">Colosseum, Italy</a></li>\n\t<li><a href=\"http://whc.unesco.org/en/list/252\">Taj Mahal, India</a></li>\n\t<li><a href=\"http://www.nationalgeographic.com/archaeology-and-history/archaeology/lost-city-petra/\">Petra, Jordan</a></li>\n</ol>\n\n<h1>Chichen Itza</h1>\n\n<p class=\"image-align-center\"><img alt=\"\" height=\"498\" src=\"https://www.ancient.eu/img/r/p/750/3048.jpg?v=1485680877\" width=\"750\"></p>\n\n<p><a href=\"https://www.ancient.eu/Chichen_Itza/\">Chichen Itza</a>, located at the northern tip of the Yucatán Peninsula of modern Mexico, was a <a href=\"https://www.ancient.eu/Maya/\">Maya</a><a href=\"https://www.ancient.eu/city/\">city</a> which was later significantly influenced by the <a href=\"https://www.ancient.eu/Toltec_Civilization/\">Toltec civilization</a>. Flourishing between c. 750 and 1200 CE, the site is rich in monumental architecture and sculpture which promote themes of militarism and displays imagery of jaguars, eagles, and feathered-serpents. Probably a capital city ruling over a confederacy of neighbouring states, Chichen Itza was one of the great Mesoamerican <a href=\"https://www.ancient.eu/cities/\">cities</a> and remains today one of the most popular tourist sites in Mexico.</p>\n\n<p>GREAT WALL OF CHINA</p>\n\n<p>The Great Wall of China is an ancient series of walls and fortifications, totaling more than 13,000 miles in length, located in northern China. Perhaps the most recognizable symbol of China and its long and vivid history, the Great Wall was originally conceived by Emperor Qin Shi Huang in the third century B.C. as a means of preventing incursions from barbarian nomads. The best-known and best-preserved section of the Great Wall was built in the 14th through 17th centuries A.D., during the Ming dynasty. Though the Great Wall never effectively prevented invaders from entering China, it came to function as a powerful symbol of Chinese civilization’s enduring strength.</p>\n\n<p class=\"image-align-center\"><img alt=\"\" height=\"192\" src=\"https://cdn.history.com/sites/2/2016/02/GettyImages-484536724-A.jpeg\" width=\"334\"></p>\n\n<p> </p>\n
实际上我必须使用本机函数按顺序显示它。我使用了swiftSoup
库,我也得到了结果,但是包含链接,li标签,分隔符行标签的文字没有到来。我在这里分享我的代码。
func parseHtml(){
print("arrBlogDesc--\(arrBlogDesc)")
if(self.arrBlogDesc.count > 0){
var yVar : CGFloat = CGFloat()
yVar = 100.00 // 20.0
var heightVar : CGFloat = 0.0//CGFloat.greatestFiniteMagnitude
for i in 0..<self.arrBlogDesc.count{
let html = self.arrBlogDesc[i]
print("str is--\(html)")
// For Text
do {
let doc: Document = try SwiftSoup.parse(html)
let links: Elements = try doc.select("a[href]")
print("links\(links)")
let srcsLinks: Elements = try doc.select("a[href]")
let srcsStringArrayLinks: [String?] = srcsLinks.array().map { try? $0.attr("href").description }
print("srcsStringArrayLinks--\(srcsStringArrayLinks)")
let text: String = try! doc.body()!.text()
print("text is--\(text)")
let srcs: Elements = try doc.select("img[src]")
let srcsStringArray: [String?] = srcs.array().map { try? $0.attr("src").description }
print("srcsStringArray--\(srcsStringArray)")
let srcsAudio: Elements = try doc.select("audio[src]")
let srcsStringArrayAudio: [String?] = srcsAudio.array().map { try? $0.attr("src").description }
print("srcsStringArrayAudio--\(srcsStringArrayAudio)")
let srcsVideo: Elements = try doc.select("video[src]")
let srcsStringArrayVideo: [String?] = srcsVideo.array().map { try? $0.attr("src").description }
print("srcsStringArrayVideo--\(srcsStringArrayVideo)")
// FOr text
if(text != ""){
heightVar = 20.0
let textView : UITextView = UITextView()
textView.frame = CGRect.init(x: 8, y: yVar, width: (self.viewCustom.bounds.size.width - 16), height: heightVar)
textView.font = UIFont.systemFont(ofSize: 16)
textView.isUserInteractionEnabled = false
textView.text = text
textView.sizeToFit()
self.viewCustom.addSubview(textView)
let h = textView.bounds.size.height
yVar = yVar + h + 8.0 //20.0
print("yVar is--\(yVar)")
print("h is--\(h)")
}
// FOr Image
if(srcsStringArray.count > 0){
heightVar = 200.0
let strURL: String = srcsStringArray[0]!
print(strURL)
let imgView : UIImageView = UIImageView()
imgView.frame = CGRect.init(x: 8, y: yVar, width: (self.viewCustom.bounds.size.width - 16), height: heightVar)
imgView.contentMode = .scaleToFill
imgView.kf.setImage(with: URL(string: strURL)!, placeholder: UIImage(named: ""))
//imgView.setImage(url: URL(string: strURL)!)
imgView.layer.borderWidth = 1.0
imgView.layer.borderColor = UIColor.lightGray.cgColor
self.viewCustom.addSubview(imgView)
yVar = yVar + heightVar + 8.0 //20.0
print("yVar is--\(yVar)")
}
// For Audio
if(srcsStringArrayAudio.count > 0){
heightVar = 50.0
let strURL: String = srcsStringArrayAudio[0]!
print(strURL)
let viewAudio : UIView = UIView()
viewAudio.frame = CGRect.init(x: 8, y: yVar, width: (self.viewCustom.bounds.size.width - 16), height: heightVar)
viewAudio.layer.borderWidth = 1.0
viewAudio.layer.borderColor = UIColor.lightGray.cgColor
self.viewCustom.addSubview(viewAudio)
// Setting Progress Bar
let btn : UIButton = UIButton()
btn.frame = CGRect.init(x: 8, y: viewAudio.bounds.size.height/2 - 20/2, width: 20, height: 20)
//btn.backgroundColor = UIColor.red
viewAudio.addSubview(btn)
btn.setImage(UIImage(named:"Play icon"), for: .normal)
//btn.addTarget(self, action: #selector(self.btnPlayPause(sender)), for: .touchUpInside)
let progressView : UIProgressView = UIProgressView()
progressView.frame = CGRect.init(x: 16, y: 0, width: viewAudio.bounds.size.width - 36 , height: 100)
progressView.backgroundColor = UIColor.blue
progressView.progressTintColor = UIColor.blue
progressView.trackTintColor = UIColor.lightGray
progressView.backgroundColor = UIColor.green
viewAudio.addSubview(progressView)
let path: String = strURL
let mp3URL = URL(string: path)
// let player : VideoPlayerViewController = VideoPlayerViewController()
// player.url = mp3URL!
// self.present(player, animated: true, completion: nil)
do
{
// 2
audioPlayer = try AVAudioPlayer(contentsOf: mp3URL!)
// 3
Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(updateAudioProgressView(progressView:)), userInfo: nil, repeats: true)
progressView.setProgress(Float(audioPlayer.currentTime/audioPlayer.duration), animated: false)
}
catch
{
print("An error occurred while trying to extract audio file")
}
yVar = yVar + heightVar + 20.0
print("yVar is--\(yVar)")
}
// For Video
if(srcsStringArrayVideo.count > 0){
heightVar = 200.0
let strURL: String = srcsStringArrayVideo[0]!
print(strURL)
// let player : VideoPlayerViewController = VideoPlayerViewController()
// player.url = URL(string: strURL)!
// self.present(player, animated: true, completion: nil)
let viewVideo : UIView = UIView()
viewVideo.frame = CGRect.init(x: 8, y: yVar, width: (self.viewCustom.bounds.size.width - 16), height: heightVar)
//** set AVPlayer frame n Layer inside viewVideo
self.viewCustom.addSubview(viewVideo)
yVar = yVar + heightVar + 8.0 //20.0
print("yVar is--\(yVar)")
}
print("yVar is--\(yVar)")
} catch Exception.Error(_, let message) {
print(message)
} catch {
print("error")
}
}
self.heightConstraint.constant = yVar
}
}