美丽的汤找不到表

如何解决美丽的汤找不到表

我正在解析此website中的数据。我已经从网站上解析了几件事,但是由于某种原因,该特定页面找不到任何表格。这是一个演示该问题的简化代码段:

#!/usr/bin/env python3 

import bs4 as bs
import requests

def get_soup(site):
    headers = {'User-Agent': 'Mozilla/5.0'}
    r = requests.get(site,headers=headers)
    #Always want a status code of 200,which means everything downloaded
    if r.status_code != 200:
        print(r.status_code)
        print("Invalid Status Code")
        exit(1)
    return bs.BeautifulSoup(r.content,'html.parser')

soup = get_soup("https://www.hockey-reference.com/boxscores/202008140WSH.html#all_advanced")
table = soup.find('table',{'id' : "NYI_skaters"}).find('tbody').find_all('tr')
table = soup.find('table',{'id' : "NYI_goalies"}).find('tbody').find_all('tr')
table = soup.find('table',{'id' : "NYI_adv"}).find('tbody').find_all('tr')

该代码可以找到没有问题的溜冰者和守门员表,但是找不到_adv表,这会导致NoneType错误,因为它没有找到该表。我可以找到_adv所在的节点:

table = soup.find('div',{'id' : "all_advanced"})

在div标签(all_advanced)下面,有一些看起来很奇怪的代码,所以我不确定这是否与它有关。我在这个特定网站上没有任何其他问题,也从未使用过硒,而且对于人们抓取数据也很好。任何帮助将不胜感激。

编辑:使用panda.read_html也找不到。我可以通过将上面的所有“ table =”替换为:

来解决此问题
for comment in soup.find_all(text=lambda text: isinstance(text,bs.Comment)):
    if comment.find("<table ") > 0:
        comment_soup = bs.BeautifulSoup(comment,'html.parser')
        table = comment_soup.find('table',{'id' : "NYI_adv"})
for player in table.find_all('tr',{'class' : "ALLSH hidden"}):
    print(player.find('a')['href'])

谢谢

解决方法

要从注释部分加载表数据,请使用以下脚本:

import requests
from bs4 import BeautifulSoup,Comment

url = 'https://www.hockey-reference.com/boxscores/202008140WSH.html#all_advanced'
soup = BeautifulSoup(requests.get(url).content,'html.parser')

# normal tables:
# table_skaters = soup.select_one('table#NYI_skaters')
# table_goalies = soup.select_one('table#NYI_goalies')

# table loaded from Comment:
table_advanced = soup.select_one('#all_advanced').find_next(text=lambda t: isinstance(t,Comment))
table_advanced = BeautifulSoup(table_advanced,'html.parser')

# print(table_advanced)

for row in table_advanced.select('tr.ALL5v5'):
    tds = [td.get_text(strip=True) for td in row.select('td,th')]
    print(*tds,sep='\t')

打印:

Josh Bailey     1       9       21      30.0    -32.1   3       1       75.0    0       1
Mathew Barzal   3       12      9       57.1    7.8     3       6       33.3    0       0
Anthony Beauvillier     4       10      21      32.3    -29.1   4       1       80.0    1       1
Derick Brassard 1       8       6       57.1    7.1     6       8       42.9    3       0
Casey Cizikas   2       14      7       66.7    20.4    4       5       44.4    1       0
Cal Clutterbuck 5       14      8       63.6    16.6    4       5       44.4    3       0
Jordan Eberle   3       14      9       60.9    13.2    3       6       33.3    1       0
Andy Greene     0       9       13      40.9    -13.6   6       8       42.9    2       1
Leo Komarov     2       10      7       58.8    9.5     7       8       46.7    5       1
Nick Leddy      2       13      20      39.4    -18.8   4       8       33.3    0       1
Anders Lee      2       13      6       68.4    22.0    2       7       22.2    0       0
Matt Martin     2       9       7       56.2    6.2     0       2       0.0     4       0
Scott Mayfield  1       16      11      59.3    11.8    4       4       50.0    3       1
Brock Nelson    2       9       19      32.1    -27.9   3       1       75.0    2       0
Jean-Gabriel Pageau     3       13      9       59.1    10.6    9       10      47.4    4       0
Adam Pelech     3       15      13      53.6    3.6     8       4       66.7    2       1
Ryan Pulock     5       18      14      56.2    8.0     6       9       40.0    0       1
Devon Toews     4       19      15      55.9    7.8     4       7       36.4    1       4
TOTAL           45      43      51.1                            44.4    32      12
Travis Boyd     3       6       7       46.2    -3.1    3       5       37.5    0       1
John Carlson    5       19      14      57.6    14.0    5       12      29.4    2       1
Brenden Dillon  2       18      19      48.6    -0.4    7       2       77.8    0       3
Nic Dowd        1       3       6       33.3    -17.3   3       0       100.0   0       1
Lars Eller      1       17      21      44.7    -7.3    6       2       75.0    1       1
Carl Hagelin    1       7       8       46.7    -2.6    5       4       55.6    1       0
Garnet Hathaway 1       4       6       40.0    -10.0   3       0       100.0   2       0
Nick Jensen     0       3       12      20.0    -34.8   8       2       80.0    0       0
Michal Kempny   3       18      12      60.0    16.9    4       10      28.6    1       0
Ilya Kovalchuk  0       5       9       35.7    -15.7   3       7       30.0    3       0
Evgeny Kuznetsov        5       16      10      61.5    18.0    8       9       47.1    1       0
Dmitry Orlov    1       23      22      51.1    4.6     8       5       61.5    1       0
T.J. Oshie      4       21      19      52.5    6.7     5       2       71.4    2       1
Alex Ovechkin   8       16      13      55.2    9.4     8       9       47.1    4       0
Richard Panik   2       7       10      41.2    -9.5    5       1       83.3    0       0
Jonas Siegenthaler      1       5       11      31.2    -21.6   8       1       88.9    2       1
Jakub Vrana     3       13      16      44.8    -6.0    3       3       50.0    0       0
Tom Wilson      2       14      10      58.3    13.0    8       6       57.1    8       0
TOTAL           43      45      48.9                            55.6    28      9

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-