首页 > 解决方案 > Beautifulsoup4 不显示表格内容

问题描述

我正在使用 Beautifulsoup4 来抓取 github 中的信息。但是,每当我尝试获取表格中的数据时,程序只会返回打开和关闭的表格标签。

from bs4 import BeautifulSoup as bs
import requests
import lxml

source = requests.get("https://github.com/bitcoin-dot-org/bitcoin.org/find/master").text
soup = bs(source, "lxml")
tbody = soup.find("tbody", class_= "js-tree-finder-results js-navigation-container js-active-navigation-container")
print(tbody)

这是它返回的内容:

<tbody class="js-tree-finder-results js-navigation-container js-active-navigation-container">
</tbody>

这是来自 github 链接的源代码(这只是与问题有关的部分):

 <tbody class="js-tree-finder-results js-navigation-container js-active-navigation-container"><tr class="js-navigation-item tree-browser-result" aria-selected="false">
              <td class="icon"><svg class="octicon octicon-chevron-right" viewBox="0 0 8 16" version="1.1" width="8" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.5 8l-5 5L1 11.5 4.75 8 1 4.5 2.5 3l5 5z"></path></svg></td>
              <td class="icon"><svg class="octicon octicon-file" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg></td>
              <td>
                <a class="css-truncate-target js-navigation-open js-tree-finder-path" href="https://github.com/bitcoin-dot-org/bitcoin.org/blob/master/.gitattributes">.gitattributes</a>
              </td>
            </tr><tr class="js-navigation-item tree-browser-result" aria-selected="false">
              <td class="icon"><svg class="octicon octicon-chevron-right" viewBox="0 0 8 16" version="1.1" width="8" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M7.5 8l-5 5L1 11.5 4.75 8 1 4.5 2.5 3l5 5z"></path></svg></td>
              <td class="icon"><svg class="octicon octicon-file" viewBox="0 0 12 16" version="1.1" width="12" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M6 5H2V4h4v1zM2 8h7V7H2v1zm0 2h7V9H2v1zm0 2h7v-1H2v1zm10-7.5V14c0 .55-.45 1-1 1H1c-.55 0-1-.45-1-1V2c0-.55.45-1 1-1h7.5L12 4.5zM11 5L8 2H1v12h10V5z"></path></svg></td>
              <td>
                <a class="css-truncate-target js-navigation-open js-tree-finder-path" href="https://github.com/bitcoin-dot-org/bitcoin.org/blob/master/.gitignore">.gitignore</a>
              </td>
            </tr></tbody>

我已经尝试使用不同的解析器,并且我尝试使用 urblib3 而不是请求来获取源代码,但是任何一种方式都会给我相同的结果。

标签: pythonweb-scrapingbeautifulsoup

解决方案


试试这个:

source = requests.get("https://github.com/bitcoin-dot-org/bitcoin.org/find/master").text
soup = bs(source, "lxml")
tbody = soup.find_all('tbody')[0]
print(tbody)

推荐阅读