【问题标题】:how do i stop beautiful soup from skipping rows while parsing?我如何阻止漂亮的汤在解析时跳过行?
【发布时间】:2011-01-24 13:22:18
【问题描述】:

使用beautifulsoup解析html中的表格时,每隔一行开始

<tr class="row_k">

而不是没有类的 tr 标签

示例 HTML

<tr class="row_k"> 
<td><img src="some picture url" alt="Item A"></td> 
<td><a href="some url"> Item A</a></td> 
<td>14.8k</td> 
<td><span class="drop">-555</span></td> 
<td> 
<img src="some picture url" alt="stuff" title="stuff"> 
</td> 
<td> 
<img src="some picture url" alt="Max llll"> 
</td> 
</tr> 
<tr> 
<td><img src="some picture url" alt="Item B"></td> 
<td><a href="some url"> Item B</a></td> 
<td>64.9k</td> 
<td><span class="rise">+165</span></td> 
<td> 
<img src="some picture url" alt="stuff" title="stuff"> 
</td> 
<td> 
<img src="some picture url" alt="max llll"> 
</td> 
</tr> 
<tr class="row_k"> 
<td><img src="some picture url" alt="Item C"></td> 
<td><a href="some url"> Item C</a></td> 
<td>4,000</td> 
<td><span class="rise">+666</span></td> 
<td> 
<img src="some picture url" title="stuff"> 
</td> 
<td> 
<img src="some picture url" alt="Maximum lllle"> 

我要提取的文本是 14.8k、64.9k 和 4,000

this1 = urllib2.urlopen('my url').read()
this_1 = BeautifulSoup(this1)
this_1a = StringIO.StringIO()
for row in this_1.findAll("tr", { "class" : "row_k" }):
  for col in row.findAll(re.compile('td')):
    this_1a.write(col.string if col.string else '')
Item_this1 = this_1a.getvalue()

我感觉这段代码写得不好,有没有更灵活的工具可以使用,比如 XML 解析器?有人可以建议。

仍然对仍然使用 beautifulsoup 的任何答案持开放态度。

【问题讨论】:

  • 如果包含更多的 html 会更容易。我认为您是在说表格正文中有一个锚标记,并且表格中锚选项卡之后的下一列包含您想要的数据。
  • 我做了一个样本。我正在使用 beautifulsoup 来执行此操作,但问题是表中的所有其他列都有 tr class="row_k" 所以它忽略它并且不会给我来自 tr 标签的信息。我会更新我的问题。

标签: python xml tags urllib2 beautifulsoup


【解决方案1】:

我还在学习很多东西,但我建议你试试 lxml。我将对此进行尝试,我认为它主要可以让你到达那里,但可能有一些我不确定的细节。

假设 this1 是一个字符串

from lxml.html import fromstring
this1_tree=fromstring(this1)
all_cells=[(item[0], item[1]) for item in enumerate(this1_tree.cssselect('td'))] # I am hoping this gives you the cells with their relative position in the document)

我唯一不能完全确定的是您是否测试每个单元格的键或值或 text_content 以确定它是否具有您在锚引用或文本中寻找的字符串。这就是为什么我想要一个你的 html 样本。但是其中一个应该可以工作

the_cell_before_numbers=[]
for cell in all_cells:
    if 'Item' in cell[1].text_content():
        the_cell_before_numbers.append(cell[0])

现在您已经有了单元格,然后可以通过获取下一个单元格的文本内容来获得所需的值

todays_price=all_cells[the_cell_before_number+1][1].text_content()

我确信有一个更漂亮的方法,但我认为这会让你到达那里。

我使用你的 html 进行了测试,我得到了你想要的东西。

【讨论】:

  • 抱歉,我是新手。我不确定如何实现这一点? =/ 我到底要把这些放在哪里?
  • 好吧,我使用的是 lxml 而不是 BeautifulSoup。所以你需要安装lxml。您需要回到这个问题的早期版本,因为我的答案是使用该描述构建的。但是这段代码应该能让你到达那里。它假定 this1 是您使用 urllib 拉入的 htm 页面,它是一个字符串对象。
  • ic,我现在的问题是另一种性质,安装 lxml 给了我一个恼人的错误。但我相信如果我最终想要的话,这会让我受益。非常感谢。
  • 关于 Microsoft Visual Basic 9 的错误以及它失败的程度以及退出状态为 2 的失败
  • 我不知道这个错误,我之前必须安装 Visual C 运行时库,但不是 VB9。您确定没有收到与 Visual C 运行时库相关的错误。如果是这样,您可以从 Microsoft 下载安装程序 microsoft.com/downloads/…
猜你喜欢
  • 2018-07-03
  • 1970-01-01
  • 2019-11-10
  • 2013-03-21
  • 2015-07-03
  • 1970-01-01
  • 2017-05-23
  • 2021-03-06
  • 1970-01-01
相关资源
最近更新 更多