python - 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 而不是请求来获取源代码,但是任何一种方式都会给我相同的结果。
解决方案
试试这个:
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)
推荐阅读
- css - CSS Burger 菜单 100 页面高度减去无固定标题
- spring-boot - 使用Nginx后如何修复所有静态资源的“服务器响应状态为404()”
- mysql - 如何删除 mySQL 中的重复项?
- python - Python 中的预测模型
- python - 根据条件更新 Pandas 数据框
- c++ - 析构函数给出“可能未初始化”用于删除资源
- seq - 在nim语言中,如何搜索对象第一次出现的索引
- javascript - 如何在jQuery中的多个选择之间获取最后一个选项值
- macos - 将 SVG 导入到 powerpoint 或 inkscape 时透明部分变黑
- docker - Docker 卷不是一个容器的完全同步目录