首页 > 解决方案 > Soup.find 和 findAll 无法在 hockey-reference.com 上找到表格元素

问题描述

我只是 webscraping 和 python 的初学者,所以如果答案很明显,我很抱歉,但我不知道我无法在https://www.hockey-上找到任何表格元素参考.com/leagues/NHL_2018.html

我最初的想法是,这是整个 div 被注释掉的结果,所以按照我在另一个类似帖子中找到的一些建议,我替换了注释字符并确认当我将 soup.text 保存到文本文件并进行搜索。但是,我仍然找不到任何桌子。

在尝试进一步搜索时,我从我的 .find 中取出了 ID 并做了一个 findAll ,但表格仍然是空的。

这是我尝试使用的代码,非常感谢任何建议!

import csv
import requests
from BeautifulSoup import BeautifulSoup
import re

comm = re.compile("<!--|-->")

url = 'https://www.hockey-reference.com/leagues/NHL_2018.html'
response = requests.get(url)
html = response.content

soup = BeautifulSoup(comm.sub("", html))
table = soup.find('table', id="stats")

在搜索我使用的所有表格元素时

table = soup.findAll('table')

我也知道网站上有一个 csv 版本,我只是渴望练习。

标签: python-2.7web-scrapingbeautifulsoup

解决方案


提供一个解析器以及您的标记,例如BeautifulSoup(html,'lxml'). 试试下面的代码

url = 'https://www.hockey-reference.com/leagues/NHL_2018.html'
response = requests.get(url)
html = response.content
soup = BeautifulSoup(html,'lxml')
table = soup.findAll('table')

推荐阅读