【发布时间】:2017-06-27 13:15:09
【问题描述】:
我使用了下面的代码,但它只能让我得到一个之间的文本,当它应该让我得到 5 <p> 和 </p> 之间的文本时
> var myHTMLString = try String(contentsOf: myURL, encoding: .ascii)
while let idRange = myHTMLString.range(of: "post-51"){
myHTMLString=myHTMLString.substring(from: idRange.upperBound)
if let paraRange = myHTMLString.range(of: "<p>"){
myHTMLString=myHTMLString.substring(from: paraRange.upperBound)
if let paraCloseRange = myHTMLString.range(of: "</p>"){
HTMLData = myHTMLString.substring(to: paraCloseRange.lowerBound)
textViewer.text = HTMLData
myHTMLString = myHTMLString.substring(from: paraCloseRange.upperBound)
}else{
//Handle paragraph close tag not found
print("Handle paragraph close tag not found")
}
}else{
//Handle paragraph start tag not found
print("Handle paragraph start tag not found")
}
}
完整的 HTML 字符串是:`
<!-- main content -->
<div id="content" class="main-content-inner col-sm-12 col-md-9">
<header>
<h1 class="page-title">Community</h1>
</header>
<article id="post-51" class="post-51 page type-page status-publish hentry">
<!-- .entry-header -->
<div class="entry-content">
<h1>Your Experience, Your Programs</h1>
<p>The Purdue Honors College is dedicated to providing meaningful opportunities to enhance the honors student experience. We are building an interdisciplinary community of scholars by adding value through specialized programming and events that are connected to our pillars. The Honors College strives to create an environment in which every student can feel connected, learn, and grow as they each pursue greatness. To reach your full potential in the Honors College, students should attend at least three honors programs per semester outside of the regular curriculum requirements. We invite you to be a part of one of our many upcoming events as we ignite the imagination of our community and forge the future of our college.</p>
<hr />
<h3>Events Calendar</h3>
<p>The Honors College hosts events to keep students engaged with their peers and the Honors College faculty.</p>
<p><a href="https://honors.purdue.edu/community/calendar/">Click here to learn more about upcoming events in the Honors College.</a></p>
<hr />
<h3>Honors College and Residences</h3>
<p>The new 324,000-square-foot Honors College and Residences is the first of its kind in the state of Indiana. It encourages scholarship and connects students with faculty while being emblematic of the Mission of the Purdue Honors College: from the locally sourced building materials to LEED certification and interactive learning spaces.</p>
<p><a href="https://honors.purdue.edu/community/honors-college-and-residences/">Click here to learn more about the new Honors College and Residences buildings.</a></p>
<hr />
<h3>Honors Network News</h3>
<p><a href="https://honors.purdue.edu/community/honors-network-news/">Click here to view the Honors Network News archive.</a></p>
<hr />
<h3>News</h3>
<p>Stay up to date with news about the Honors College. Learn about the awesome things our students are doing and follow the Honors College on social media.</p>
<p><a href="https://honors.purdue.edu/community/news/">Click here to view more news about the Honors College.</a></p>
<hr />
<h3>Photo Gallery</h3>
<p><a href="https://honors.purdue.edu/community/photo-gallery/">Click here to view photos of Honors College events.</a></p>
<hr />
<h3>Published Works</h3>
<p><a href="https://honors.purdue.edu/community/published-works/">Click here to view the published works of the Honors College.</a></p>
<hr />
<h3>Signature Programs</h3>
<p><a href="https://honors.purdue.edu/community/signature-programs/">Click here to learn more about Signature Programs from the Honors College.</a></p>
<hr />
</div><!-- .entry-content -->
</article><!-- #post-## -->
`
【问题讨论】:
-
是的。你可以,试试 $("#p id ").text() 使用 jquery
-
这很快。我认为 jquery 不适用于 Swift。我说的对吗?
-
糟糕,我没注意到。
-
你要解析html页面吗?
-
@SergeyDi 我试图从位于
post-51的 HTML ID 下的网页中获取文本。有多个<p>和</p>,我想从它们中获取文本并打印出来
标签: html swift parsing text textview