python-2.7 - 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 版本,我只是渴望练习。
解决方案
提供一个解析器以及您的标记,例如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')
推荐阅读
- azure - 将 B2C 重定向 URL 设置为 b2clogin.com 时,我在哪里可以将 validateAuthority 设置为 false
- python - 考虑不确定性的高斯拟合
- asp.net-mvc - 防止在 ASP.NET MVC 中为某些视图搭建脚手架
- angularjs - 根据自定义 ui-grid 上的值启用和禁用单元格选择
- javascript - ReCaptcha V3 Invisible: Uncaught (in promise) 请求超时
- python - 如何迭代 jinja 属性:错误没有属性
- angular - angular 7 新项目:错误!代码 ETARGET ; 找不到与 @angular-devkit/ 匹配的版本
- r - R:修复定义为函数的循环
- postgresql - 如何引发自定义 Postgresql 错误并在 Ecto 中处理它
- c# - .NET Core - 附件导致发送邮件失败