如何让美丽的汤类似的标签? [英] How to get similar tags in beautiful soup?

查看:326
本文介绍了如何让美丽的汤类似的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < D​​IV ID =上市的细节>
< D​​IV ID =上市的细节清单>
  < H3类=挂牌的细节头>
  < UL类=重要的字段>
  < UL>
    <立GT;
      <跨度>物业参考:LT; / SPAN>
      <强> BLJLT / HSN / 02022014 / BNJM / DD< / STRONG>
    < /李>
    <立GT;
      <跨度>家具:< / SPAN>
      <强>否LT; / STRONG>
    < /李>
    <立GT;
      <跨度>上市:其中; / SPAN>
      <强>代理< / STRONG>
    < /李>
  < / UL>
  < UL>
    <立GT;
      <跨度>租金支付:其中; / SPAN>
      <强>年度< / STRONG>
    < /李>
    <立GT;
      <跨度>大厦:< / SPAN>
      <强>阿拉伯塔Nujoom< / STRONG>
    < /李>
    <立GT;
      <跨度>设施:LT; / SPAN>
      <强>中央的A / C&放大器;暖气,阳台,建于衣柜,安全性,地标和LT观; / STRONG>
    < /李>
    <李ID =的详细信息,代理信息>
      <跨度>上市:其中; / SPAN>
      <强>
    < /李>
  < / UL>
< / DIV>

我希望得到上述所有标记,使用

 为在soup.select(#房源​​的细节列表里跨度):
    打印spans_others.append(a.text)

,但它不工作。谁能告诉我是什么问题?我使用Python中美味的汤。


解决方案

 为在soup.select(#房源​​的细节列表里跨度):

有与这条线没问题,假设你试图让所有的跨度上市的细节列表 ID。参见:

 为在soup.select(#房源​​的细节列表里跨度):
    打印一
<跨度>物业参考:LT; / SPAN>
<跨度>家具:< / SPAN>
<跨度>上市:其中; / SPAN>
<跨度>租金支付:其中; / SPAN>
<跨度>大厦:< / SPAN>
<跨度>设施:LT; / SPAN>
<跨度>上市:其中; / SPAN>

您的问题是,你正试图打印的结果的追加电话:

 打印spans_others.append(a.text)

list.append 总是返回(发生变异的名单,它没有收益任何东西)。

  A = [] .append(4)一个是无
出[19]:真

因此​​,无论打印 spans_others 你完成追加到它之后,或打印(一),或等

<div id="listing-details">
<div id="listing-details-list">
  <h3 class="listing-details-header">
  <ul class="important-fields">
  <ul>
    <li>
      <span> Property Reference: </span>
      <strong> BLJLT/HSN/02022014/BNJM/DD </strong>
    </li>
    <li>
      <span> Furnished: </span>
      <strong> No </strong>
    </li>
    <li>
      <span> Listed By: </span>
      <strong> Agent </strong>
    </li>
  </ul>
  <ul>
    <li>
      <span> Rent Is Paid: </span>
      <strong> Yearly </strong>
    </li>
    <li>
      <span> Building: </span>
      <strong> Burj Al Nujoom </strong>
    </li>
    <li>
      <span> Amenities: </span>
      <strong> Central A/C & Heating , Balcony , Built in Wardrobes , Security , View of Landmark </strong>
    </li>
    <li id="details-agent-info">
      <span>Listed by:</span>
      <strong>
    </li>
  </ul>
</div>

I want to get all the above tags, using

for a in soup.select("#listing-details-list li span"):
    print spans_others.append(a.text)

but it doesn't work. Can anyone tell me what the problem is? I am using Beautiful Soup in Python.

解决方案

for a in soup.select("#listing-details-list li span"):

There is no problem with this line, assuming you're trying to get all the span tags under the listing-details-list id. See:

for a in soup.select("#listing-details-list li span"):
    print a
<span> Property Reference: </span>
<span> Furnished: </span>
<span> Listed By: </span>
<span> Rent Is Paid: </span>
<span> Building: </span>
<span> Amenities: </span>
<span>Listed by:</span>

Your problem is that you are trying to print the results of an append call:

print spans_others.append(a.text)

and list.append always returns None (it mutates the list, it doesn't return anything).

a = [].append(4)

a is None
Out[19]: True

So either print spans_others after you're done appending to it, or print(a), or etc.

这篇关于如何让美丽的汤类似的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆