【问题标题】:Parsing html in with BeautifulSoup fails to find a table用 BeautifulSoup 解析 html 找不到表
【发布时间】:2017-03-12 21:43:05
【问题描述】:

我正在尝试解析此网站中的数据: http://www.baseball-reference.com/boxes/CHN/CHN201606020.shtml

我想提取表中的一些数据。但由于某种原因,我很难找到它们。比如我想做的就是这个

from bs4 import BeautifulSoup
import requests

url = 'http://www.baseball-reference.com/boxes/CHN/CHN201606020.shtml'
soup = BeautifulSoup(requests.get(url).text)
soup.find('table', id='ChicagoCubsbatting')

尽管 html 中存在具有该 id 的表,但最后一行不返回任何内容。此外,即使页面中有很多表,len(soup.findAll('table')) 也会返回 1。我试过使用“lxml”、“html.parser”和“html5lib”。所有的行为方式都一样。

发生了什么事?为什么这不起作用,我该怎么做才能提取表格?

【问题讨论】:

  • 该表在评论中,因此它实际上不是文档的一部分。
  • 那它是如何出现在网页中的呢?即便如此,我该如何提取它?

标签: python html beautifulsoup


【解决方案1】:

使用soup.find('div', class_='placeholder').next_sibling.next_sibling 获取评论文本,然后使用这些文本构建一个新的soup

In [35]: new_soup = BeautifulSoup(text, 'lxml')

In [36]: new_soup.table
Out[36]: 
<table class="teams poptip" data-tip="San Francisco Giants at Atlanta Braves">
<tbody>
<tr class="winner">
<td><a href="/teams/SFG/2016.shtml">SFG</a></td>
<td class="right">6</td>
<td class="right gamelink">
<a href="/boxes/ATL/ATL201606020.shtml">Final</a>
</td>
</tr>
<tr class="loser">
<td><a href="/teams/ATL/2016.shtml">ATL</a></td>
<td class="right">0</td>
<td class="right">
</td>
</tr>
</tbody>
</table

【讨论】:

  • 这行得通,但是任何人都可以解释注释掉的表格是如何/为什么最终在浏览器中呈现而不是从 BeautifulSoup 呈现的吗?
  • @dillon 这是 JavaScript
猜你喜欢
  • 2011-09-24
  • 2014-09-03
  • 2011-05-10
  • 2011-01-04
  • 2012-01-12
  • 1970-01-01
  • 2012-12-13
  • 2011-06-15
  • 1970-01-01
相关资源
最近更新 更多