Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Web scraping Python BeautifulSoup Web Scrape在Div标记中_Web Scraping_Python 3.6 - Fatal编程技术网

Web scraping Python BeautifulSoup Web Scrape在Div标记中

Web scraping Python BeautifulSoup Web Scrape在Div标记中,web-scraping,python-3.6,Web Scraping,Python 3.6,我的头撞到墙上了。我已经成功地解析出了这个页面上的大多数表,没有问题,但是标记one让我感到困惑 页面=https://www.hockey-reference.com/teams/TBL/2018.html 这: 给我要找的div标签,我想我可以把桌子拿出来: using table_body = table_div.find('tbody') 但那不会有任何回报,我真的不知道为什么。如果我做一个简单的例子: for row in table_div: if 'tbody'

我的头撞到墙上了。我已经成功地解析出了这个页面上的大多数表,没有问题,但是
标记one让我感到困惑

页面=
https://www.hockey-reference.com/teams/TBL/2018.html

这:

给我要找的div标签,我想我可以把桌子拿出来:

using table_body = table_div.find('tbody')
但那不会有任何回报,我真的不知道为什么。如果我做一个简单的例子:

for row in table_div:
        if 'tbody' in row:
            print ("Found")
我被“发现”了,所以它很明显就在那里,为什么找不到呢,找到那篇文章

上述过程适用于所有其他表,为什么不适用于此表?我甚至试着寻找TRs。。。这就像
table\u div
不是find希望处理的东西


我讨厌寻求帮助,但这一点我真的不知所措。帮我看看我遗漏了什么。

您要查找的表位于html注释中
。对象是
navigablesting
的一种类型,因此不能使用
.find
或任何其他
标记方法

但是,您可以选择注释并从中构建新的
BeautifulSoup
对象

from bs4 import Comment

table_div = soup.find('div', {'class':'table_wrapper', 'id':'all_stats_adv_rs'})
comment = table_div.find(string=lambda text:isinstance(text, Comment))
soup2 = BeautifulSoup(comment, 'html.parser')

print(soup2.find('tbody'))

我重新登录说我知道了!没有你那么好,但是是的,这是一个危险的评论!我“修复”它的方法是将文件作为行列表循环,然后删除所有注释标记('s)
from bs4 import Comment

table_div = soup.find('div', {'class':'table_wrapper', 'id':'all_stats_adv_rs'})
comment = table_div.find(string=lambda text:isinstance(text, Comment))
soup2 = BeautifulSoup(comment, 'html.parser')

print(soup2.find('tbody'))