Beautifulsoup4不显示表内容

时间:2019-03-05 18:52:55

标签: python web-scraping beautifulsoup

我正在使用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代替获取源代码的请求,但是无论哪种方式都可以得到相同的结果。

2 个答案:

答案 0 :(得分:0)

尝试一下:

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)

答案 1 :(得分:0)

可能您搜索的class属性值不正确。请尝试下面的class属性值。

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-browser-result-template")
print(tbody)

输出:

    <tbody class="js-tree-browser-result-template" hidden="">
    <tr class="js-navigation-item tree-browser-result">
    <td class="icon"><svg aria-hidden="true" class="octicon octicon-chevron-right" height="16" version="1.1" viewbox="0 0 8 16" width="8"><path d="M7.5 8l-5 5L1 11.5 4.75 8 1 4.5 2.5 3l5 5z" fill-rule="evenodd"></path></svg></td>
    <td class="icon"><svg aria-hidden="true" class="octicon octicon-file" height="16" version="1.1" viewbox="0 0 12 16" width="12"><path 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" fill-rule="evenodd"></path></svg></td>
    <td>

<a class="css-truncate-target js-navigation-open js-tree-finder-path" href="/bitcoin-dot-org/bitcoin.org/blob/master"></a>
</td>
</tr>
</tbody>