【问题标题】:Beautifulsoup4 does not show the table contentBeautifulsoup4 不显示表格内容
【发布时间】:2019-03-05 18:52:54
【问题描述】:

我正在使用 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 而不是 requests 来获取源代码,但任何一种方式都会给我相同的结果。

【问题讨论】:

  • 访问 .text 属性,即 tbody.text
  • 表格和表格内容是用javascript创建的,beautifulsoup无法生成,您可能需要查看Selenium或PhantomJS之类的库来获取javascript表格内容

标签: python web-scraping beautifulsoup


【解决方案1】:

可能您搜索错误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>

【讨论】:

    【解决方案2】:

    试试这个:

    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)
    

    【讨论】:

      猜你喜欢
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多