【发布时间】:2016-04-22 22:02:41
【问题描述】:
如何从结构不好的 html 中获取段落?
我有这个原始的 html 文本:
This position is responsible for developing and implementing age appropriate lesson and activity plans for preschool children, ages 4-5 years-old. Maintain a fun and interactive classroom that is clean and well organized, provide a safe, healthy and welcoming learning environment. The ideal candidate will have:
<br>
<ul>
<li>AA Early Childhood Education, or related field. </li>
<li>2+ years experience in a licensed childcare facility </li>
<li>Ability to meet state requirements, including finger print clearance. </li>
<li>Excellent oral and written communication skills </li>
<li>Strong organization and time management skills. </li>
<li>Creativity in expanding children's learning through play.<br> </li>
<li>Strong classroom management skills.<br> </li>
</ul>
<p>The ideal candidate must be a reliable, self-starting professional who is passionate about teaching young children.
<br>
</p>
我使用 Python 并尝试做类似的事情:
soup = BeautifulSoup(html)
它返回一个包含 2 个 short 段落的新 html 文本:
<html>
<body>
<p>This position is responsible for developing and implementing age appropriate lesson and activity plans for preschool children, ages 4-5 years-old. Maintain a fun and interactive classroom that is clean and well organized, provide a safe, healthy and welcoming learning environment. The ideal candidate will have:
<br/>
</p>
<ul>
<li>AA Early Childhood Education, or related field. </li>
<li>2+ years experience in a licensed childcare facility </li>
<li>Ability to meet state requirements, including finger print clearance. </li>
<li>Excellent oral and written communication skills </li>
<li>Strong organization and time management skills. </li>
<li>Creativity in expanding children's learning through play.
<br/> </li>
<li>Strong classroom management skills.
<br/> </li>
</ul>
<p>The ideal candidate must be a reliable, self-starting professional who is passionate about teaching young children.
<br/> </p>
</body>
</html>
但这不是我所期望的。结果,我想得到这个html文本:
<html>
<body>
<p>This position is responsible for developing and implementing age appropriate lesson and activity plans for preschool children, ages 4-5 years-old. Maintain a fun and interactive classroom that is clean and well organized, provide a safe, healthy and welcoming learning environment. The ideal candidate will have:
AA Early Childhood Education, or related field.
2+ years experience in a licensed childcare facility
Ability to meet state requirements, including finger print clearance.
Excellent oral and written communication skills
Strong organization and time management skills.
Creativity in expanding children's learning through play.
Strong classroom management skills.
</p>
<p>The ideal candidate must be a reliable, self-starting professional who is passionate about teaching young children.</p>
</body>
</html>
为了获得以上html,我认为最好的方法是从原始html中删除除<p>和</p>之外的所有html标签。
为此,我尝试了以下正则表达式:
new_html = re.sub('<[^<]+?>', '', html)
显然,正则表达式会删除所有 html 标签。那么,如何去除除<p>和</p>之外的所有html标签?
如果有人帮我写 r.e.然后我将new_html 提供给BeautifulSoup() 并获得我期望的html。
【问题讨论】:
-
您要检索文本吗?如果是,那么
soup.get_text()应该没问题。 -
不,我想检索段落列表。
-
那些
li标签呢?你想用文本替换它们吗? -
是的,并将它们添加到第一段中。
标签: python html regex beautifulsoup