【发布时间】:2017-10-15 20:16:25
【问题描述】:
我有以下html:
<div id="contentDiv">
<!-- START FILER DIV -->
<div style="margin: 15px 0 10px 0; padding: 3px; overflow: hidden; background-color: #BCD6F8;">
<div class="mailer">Mailing Address
<span class="mailerAddress">500 ORACLE PARKWAY</span>
<span class="mailerAddress">MAIL STOP 5 OP 7</span>
<span class="mailerAddress">REDWOOD CITY CA 94065</span>
</div>
我正在尝试访问“500 ORACLE PARKWAY”和“MAIL STOP 5 OP &”,但找不到方法。我的尝试是这样的:
for item in soup.findAll("span", {"class" : "mailerAddress"}):
if item.parent.name == 'div':
return_list.append(item.contents)
编辑:我忘了提到在 html 中之后的元素使用相似的标签,所以当我只想要前两个时它会捕获所有这些。
【问题讨论】:
-
您遇到什么样的错误?我试过你的代码,我可以看到你能够检索每个 span 元素中的文本。
-
能把HTML代码的链接发一下吗?
-
当您提供链接的页面上有一个非常好的 XML 文档时,您为什么要尝试解释 HTML:sec.gov/cgi-bin/…。 Beautiful Soup 永远应该是最后的选择。
-
不幸的是,我只能使用这个 html 而不能使用 XML 哈哈。
标签: python html beautifulsoup html-parsing