我试图从这个网页(http://www.basketball-reference.com/teams/CHO/2017.html)提取与表(teammisc)相对应的所有元素。在
我想从“Team”中提取所有数字(这行: 17 13 2.17-0.51 1.66 106.9 104.7 96.5.300.319.493 10.9 20.5.228.501 11.6 79.6.148光谱中心269,47)
import urllib2
from bs4 import BeautifulSoup
htmla = urllib2.urlopen('http://www.basketball-reference.com/teams/CHO/2017.html')
bsObja=BeautifulSoup(htmla,"html.parser")
tables = bsObja.find_all("table")
尝试了上面的代码,希望我能得到所有表的列表,然后选择正确的一个。但现在不管我怎么努力,我只能从这一页得到一张桌子。在
有什么别的办法吗?在
BS中Comment对象中的数据,而Comment对象只是NavigableString的一种特殊类型,您需要做的是:
找到包含信息的刺
使用BeautifulSoup将字符串转换为BS object
从BS object提取数据
代码:
这将返回包含表html代码的sting。在
^{pr2}$使用sting构造BS对象,并从对象中提取数据
退出:
更多评论:
注释对象只是一种特殊类型的NavigableString,BS会从中提取字符串,我们不需要更改或替换任何html。在
基于此,我们可以使用纯BS而不是
re
来提取注释如果要查找所有表字符串,可以执行以下操作:
我想你想
这个页面将所有的表隐藏在注释中,JavaScript使用它来显示表,并可能在显示前进行排序或过滤。在
所有注释都在
<div class='placeholder'>
之后,因此您可以使用它来查找此注释,从注释中获取所有文本并使用BS来解析它。在这样我发现了11个隐藏在注释中的表。在
相关问题 更多 >
编程相关推荐