首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BeautifulSoup不能完美地解析

BeautifulSoup不能完美地解析
EN

Stack Overflow用户
提问于 2015-08-07 07:15:52
回答 1查看 49关注 0票数 0

当我使用soup.find("h3", text="Main Address:").find_parents("section")时,我得到的输出是:

代码语言:javascript
复制
[<section class="otlnrw" itemscope="" itemtype="http://microformats.org/wiki/hCard">\n<header>\n<h3 i
temprop="name">Main Address:</h3>\n</header>\n<p>600 Dexter <abbr title="Avenue\r"><abbr title="Avenu
e\r">Ave.</abbr></abbr><br/><span class="locality">Montgomery</span>, <span class="region">AL</span>,
 <span class="postal-code">36104</span></p> </section>]

现在我只想打印段落的文本。我不能那样做。请告诉我如何从这里只打印这一节的这一段内的文本。

或者我的HTML页面是这样的:

代码语言:javascript
复制
<article>
<header>
    <h2 id="state-government">State Government</h2>
</header>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otln">
    <header><h3  itemprop="name">Official Name:</h3></header>
    <p><a href="http://alaska.gov/">Alaska</a>
    </p>
</section>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otlnrw">
    <header><h3  class="org">Governor:</h3></header>
    <p><a href="http://gov.alaska.gov/Walker/contact/email-the-governor.html">Bill Walker</a></p>
</section>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otln">
    <header><h3  itemprop="name">Main Address:</h3></header>
    <p>120 East 4th Street<br>
        <span class="locality">Juneau</span>, 
        <span class="region">AK</span>, 
        <span class="postal-code">99801</span></p>
</section>
<section itemscope="" itemtype="http://microformats.org/wiki/hCard" class="otlnrw">
    <header><h3  itemprop="name">Phone Number:</h3></header>
    <p class="spk tel">907-465-3708</p>
</section>
<p class="volver clearfix"><a href="#skiptarget">
    <span class="icon-backtotop-dwnlvl">Back to Top</span></a></p>
<section>
    <header><h2 id="state-agencies">State Agencies</h2></header>
    <ul>
        <li><a href="/state-consumer/alaska">Consumer Protection Offices</a></li>
        <li><a href="http://www.correct.state.ak.us/">Corrections Department</a></li>
        <li><a href="http://www.elections.alaska.gov/">Election Office</a></li>
        <li><a href="http://doa.alaska.gov/dmv/">Motor Vehicle Offices</a></li>
        <li><a href="http://doa.alaska.gov/dgs/property/">Surplus Property Sales</a></li>
        <li><a href="http://www.travelalaska.com">Travel and Tourism</a></li>
    </ul>
</section>
<p class="volver clearfix"><a href="#skiptarget">
    <span class="icon-backtotop-dwnlvl">Back to Top</span></a></p>
</article>

我应该怎样才能得到它的地址仅文本。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-07 07:35:49

当前代码返回一个包含一个元素的列表。要在其中获取<p>元素,可以对其进行一些扩展:

代码语言:javascript
复制
soup.find("h3", text="Main Address:").find_parents("section")[0]("p")

如果要获取p元素中的内容,则必须再次获得该列表的第一个元素,并在其上运行decode_contents:

代码语言:javascript
复制
soup.find("h3", text="Main Address:").find_parents("section")[0]("p")[0].decode_contents(formatter="html")

就您的情况而言,它将返回:

代码语言:javascript
复制
u'120 East 4th Street<br/><span class="locality">Juneau</span>, <span class="region">AK</span>, <span class="postal-code">99801</span>'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31871742

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档