【问题标题】:BeautifulSoup returns empty td tagsBeautifulSoup 返回空的 td 标签
【发布时间】:2018-03-13 15:29:40
【问题描述】:

我正在尝试从这个网址“http://baloncestoenvivo.feb.es/Game/1881578”获取一些信息。我想用这个id = "keyfacts-playbyplay-content-scroll"

获取一个表内的所有信息,该表位于一个div内

我使用此代码访问此表:

table = page_soup.find(id="keyfacts-playbyplay-content-scroll").findAll("table", {"class" : "twelve even"})

然后,打印“table”以查看我得到了什么,我得到一个没有数据的 tr。但是,使用 firefox 或 chrome 控制台我们可以看到有 799 个表行有数据!!!

这是我在 python 控制台中打印“表格”时得到的结果:

>> table
<table class="twelve even">
<thead>
<tr>
<th colspan="2">Tiempo</th>
<th colspan="2">Cuarto</th>
<th colspan="2">Puntuación</th>
<th colspan="8">Acción</th>
</tr>
</thead>
<tbody>
<!-- ko foreach: LINES -->
<tr>
<td class="text-center" colspan="2" data-bind="text : time"></td>
<td class="text-center" colspan="2" data-bind="text : quarter"></td>
<td colspan="2" data-bind="text : scoreA()==null ? '' : scoreA()+'-'+scoreB()" style="color:#FB0127; text-align: center"></td>
<td colspan="8" data-bind="text : text"></td>
</tr>
<!-- /ko -->
</tbody>
</table>

这就是我们在控制台中可以看到的:

为什么不一样呢?所有带有 td 标签的 tr 标签都带有信息?

我做错了什么?

【问题讨论】:

    标签: python html python-3.x beautifulsoup


    【解决方案1】:

    表格的内容是通过 JavaScript 动态生成的。这就是页面源没​​有它们的原因。 requests 模块在不执行 JavaScript 的情况下为您获取页面源,这就是您看到不完整数据的原因。

    如果您检查开发工具中Network 选项卡下的XHR 选项卡,则会向http://baloncestoenvivo.feb.es/api/KeyFacts/1881578 发送请求,该请求以JSON 形式返回数据。您可以使用requests 模块及其内置的.json() 函数来解析这些数据。

    唯一的问题是,您需要传递以下标头。没有它们,网站会阻止脚本,你会看到requests.exceptions.ConnectionError

    import requests
    
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
               'Accept': 'application/json, text/javascript, */*; q=0.01'}
    
    r = requests.get('http://baloncestoenvivo.feb.es/api/KeyFacts/1881578', headers=headers)
    data = r.json()
    

    您现在可以从data 变量中获取所有表值。要查看其结构,请使用pprint 模块。

    例如,要获取玩家姓名和对应积分,可以这样:

    for player in data['SCOREBOARD']['TEAM'][0]['PLAYER']:
        name = player['name']
        points = player['pts']
        print(name, points)
    

    输出:

    A. ELONU 6
    L. NICHOLLS GONZALEZ 10
    S. DOMINGUEZ FERNANDEZ 13
    L. QUEVEDO CAÑIZARES 0
    M. ASURMENDI VILLAVERDE 5
    F. ABDI 0
    E. DE SOUZA MACHADO 13
    L. GIL COLLADO 0
    K. GIVENS 12
    D. MOSS 2
    A. ROBINSON 0
    

    【讨论】:

      【解决方案2】:

      背后的原因是我们需要使用像Selenium这样的浏览器模拟器来渲染这个javascript生成的动态内容。 如果我们尝试仅通过请求来请求此数据,我们将无法获得您正在寻找的tds。我会推荐官方 Selenium 文档或这个库的 Youtube 教程,一旦你掌握了它就非常容易使用。

      Selenium Documentation

      from bs4 import BeautifulSoup
      import requests
      
      
      asdf = requests.get('http://baloncestoenvivo.feb.es/Game/1881578').text
      soup = BeautifulSoup(asdf, 'lxml')
      
      
      tabl = soup.find('div',{'id':'keyfacts-playbyplay-content-scroll'}).find('div',{'class':'twelve columns'})
      
      print(tabl)
      

      这不起作用,它只会返回不包含您要查找的信息的部分 HTML(即表格元素)

      【讨论】:

      • 嗨!!!我要感谢您的帮助,但最后对我来说获取 json 更有用。非常感谢您的宝贵帮助!!!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-01
      • 2012-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-07
      • 1970-01-01
      相关资源
      最近更新 更多