gpt4 book ai didi

html - 我可以获取某个 ID 中每个

之间的所有文本吗

转载 作者:行者123 更新时间:2023-11-28 06:17:12 25 4
gpt4 key购买 nike

我使用了下面的代码,但它只能让我得到一个之间的文本,而它应该让我得到 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-## -->
`

最佳答案

在找到 id 后,将您的代码更改为循环遍历所有

。看看我的评论,在满足特定条件后打破 while 循环非常重要。

 var myHTMLString = try String(contentsOf: myURL, encoding: .ascii)
if let idRange = myHTMLString.range(of: "post-51"){
myHTMLString=myHTMLString.substring(from: idRange.upperBound)
while 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
//AFTER YOU GET THE NEEDED INFORMATION, DO A break HERE to get out of while loop or you will loop through all <p>
myHTMLString = myHTMLString.substring(from: paraCloseRange.upperBound)

}else{
//Handle paragraph close tag not found
print("Handle paragraph close tag not found")
}
}
}else{
print("Handle id not found")
}

关于html - 我可以获取某个 ID 中每个 <p> 和 </p> 之间的所有文本吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44781270/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com