【问题标题】:Python and Beautifulsoup: strange behaviour with findAllPython 和 Beautifulsoup:findAll 的奇怪行为
【发布时间】:2014-03-27 19:36:45
【问题描述】:
soup        = BeautifulSoup(html)
boxes       = soup.findAll("div", { "class" : re.compile(r'\bmixDesc\b') })

我想我只有“mixDesc”类的盒子。

所以我正在调试以确保

count = 0
for box in boxes :
    count = count + 1

    print "JWORG box {0}".format(count)
    print "JWORG box len {0}".format(len(box))
    print box

我在解析的 html 文件中只有 10 个带有 mixDesc 类的 div

但我有 30 个盒子,很多(30 个中有 20 个)打印为

[]

你能解释一下为什么会这样吗?为什么 findAll 抓住这个空标签? 或者......我还犯了什么错误?

编辑 1:

我正在使用它来编写 xbmc 插件,所以我使用的是唯一可用的版本

编辑 2:

我无法复制/粘贴所有 html,但我正在抓取此页面:http://www.jw.org/it/video/?start=70

所以你可以查看 html 源代码来帮助我。

编辑 3: 这是我的 xbmc 日志,请不要我打印了一个计数器和 len(box)

20:27:54 T:5356  NOTICE: JWORG box 1
20:27:54 T:5356  NOTICE: JWORG box len 5
20:27:54 T:5356  NOTICE: [<div class="syn-img sqr mixDesc">
                                            <a href="/it/cosa-dice-la-Bibbia/famiglia/bambini/diventa-amico-di-geova/cantici/120-felice-chi-mette-in-pratica-ci%C3%B2-che-ode/" class="jsDownload jsVideoModal jsCoverDoc" data-jsonurl="/apps/TRGCHlZRQVNYVrXF?output=json&amp;pub=pksn&amp;fileformat=mp4&amp;alllangs=1&amp;track=120&amp;langwritten=I&amp;txtCMSLang=I" data-coverurl="/it/cosa-dice-la-Bibbia/famiglia/bambini/diventa-amico-di-geova/cantici/120-felice-chi-mette-in-pratica-ci%C3%B2-che-ode/" data-onpagetitle="Cantico 120: Felice chi mette in pratica ciò che ode" title="Play o download | Cantico 120: Felice chi mette in pratica ciò che ode" data-mid="1102013357">
                                            <span class="jsRespImg" data-img-type="sqr" data-img-size-lg="http://assets.jw.org/assets/m/ijw13pk/1102013357/ijw13pk_id-1102013357.art/1102013357_univ_sqr_lg.jpg" data-img-size-md="http://assets.jw.org/assets/m/ijw13pk/1102013357/ijw13pk_id-1102013357.art/1102013357_univ_sqr_md.jpg" data-img-size-sm="http://assets.jw.org/assets/m/ijw13pk/1102013357/ijw13pk_id-1102013357.art/1102013357_univ_sqr_sm.jpg" data-img-size-xs="http://assets.jw.org/assets/m/ijw13pk/1102013357/ijw13pk_id-1102013357.art/1102013357_univ_sqr_xs.jpg"></span></a><noscript><img src="http://assets.jw.org/assets/m/ijw13pk/1102013357/ijw13pk_id-1102013357_I/1102013357_univ_sqr_xs.jpg" alt="" /></noscript>

                                            <div style="display:none;" class="jsVideoPoster mid1102013357" data-src="http://assets.jw.org/assets/m/ijw13pk/1102013357/ijw13pk_id-1102013357.art/1102013357_univ_lsr_lg.jpg" data-alt=""></div>
                                            </div>]
20:27:54 T:5356  NOTICE: JWORG box 2
20:27:54 T:5356  NOTICE: JWORG box len 7
20:27:54 T:5356  NOTICE: []
20:27:54 T:5356  NOTICE: JWORG box 3
20:27:54 T:5356  NOTICE: JWORG box len 7
20:27:54 T:5356  NOTICE: []

编辑 4:

好的,有 30 个 div,因为它们是嵌套的,但为什么它们是空的?以及如何过滤掉这些?

【问题讨论】:

  • 至少,你最好使用BeautifulSoup4find_all()
  • 我正在使用 python 在 XBMC 上编写插件。我无法复制/粘贴所有 html,但我正在抓取此页面:jw.org/it/video/?start=70
  • 你能粘贴你的三行for循环的完整输出吗?
  • 肥皂也可以很漂亮,对吧? (看标题):)
  • 大声笑,已修复。即使在编写代码时也会出现此错误

标签: python beautifulsoup


【解决方案1】:

问题在于,默认情况下findAll() 执行递归搜索,并且由于嵌套的 div 包含 bmixDesc 类 - 你会得到这些结果。

recursive=False 传递给findAll 并使用id=videosIndexList 在父div 中搜索div。

而且,BeautifulSoup3 也不再维护 - 切换到 BeautifulSoup4 并使用 find_all()

下面是代码的样子(使用BeautifulSoup4):

import re
from urllib2 import urlopen
from bs4 import BeautifulSoup


soup = BeautifulSoup(urlopen('http://www.jw.org/it/video/?start=70'))

div = soup.find('div', {'id': 'videosIndexList'})
boxes = div.find_all("div", { "class" : re.compile(r'\bmixDesc\b')}, recursive=False)

for box in boxes:
    print box.text

这只会让您获得顶级 div(10 个框)。

【讨论】:

  • 但是为什么嵌套的 div 打印为空...?
  • soup.findAll("div", { "class" : re.compile(r'\bmixDesc\b') }, recursive = False) 给了我零结果。另外,我没有选择bs版本,我只有xbmc环境下的v3
  • @realtebo 不,你应该在div 上使用videosIndexList id 调用findAll(参见我的示例)。
  • @realtebo 它将为您提供 10 个顶级框。
  • @realtebo 这是因为一些嵌套的 div 不包含任何元素,例如页面源代码中的 &lt;div class="syn-img sqr mixDesc noImage"&gt;&lt;/div&gt;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-22
  • 2013-04-01
  • 1970-01-01
相关资源
最近更新 更多