如何解析特定td标记之间的html文件

时间:2017-07-12 00:17:10

标签: python html beautifulsoup html-parsing

我有一个html文件如下:

<table class="files js-navigation-container js-active-navigation-container" data-pjax>


    <tbody>
      <tr class="warning include-fragment-error">
        <td class="icon"><svg aria-hidden="true" class="octicon octicon-alert" height="16" version="1.1" viewBox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M8.865 1.52c-.18-.31-.51-.5-.87-.5s-.69.19-.87.5L.275 13.5c-.18.31-.18.69 0 1 .19.31.52.5.87.5h13.7c.36 0 .69-.19.86-.5.17-.31.18-.69.01-1L8.865 1.52zM8.995 13h-2v-2h2v2zm0-3h-2V6h2v4z"/></svg></td>
        <td class="content" colspan="3">Failed to load latest commit information.</td>
      </tr>

        <tr class="js-navigation-item">
          <td class="icon">
            <svg aria-hidden="true" class="octicon octicon-file-directory" height="16" version="1.1" viewBox="0 0 14 16" width="14"><path fill-rule="evenodd" d="M13 4H7V3c0-.66-.31-1-1-1H1c-.55 0-1 .45-1 1v10c0 .55.45 1 1 1h12c.55 0 1-.45 1-1V5c0-.55-.45-1-1-1zM6 4H1V3h5v1z"/></svg>
            <img alt="" class="spinner" height="16" src="https://assets-cdn.github.com/images/spinners/octocat-spinner-32.gif" width="16" />
          </td>
          <td class="content">
            <span class="css-truncate css-truncate-target"><a href="/opencv/opencv_contrib/tree/master/.github" class="js-navigation-open" id="01777e4a9846fea5f3fcc8fe40d44680-42b42934be959dcedc7072a2040903d29a440f03" title=".github">.github</a></span>
          </td>
          <td class="message">
            <span class="css-truncate css-truncate-target">
                  <a href="/opencv/opencv_contrib/commit/823dea726f260e07b47fb1faf446f4e35a255a6f" class="message" data-pjax="true" title="migration: github.com/opencv/opencv_contrib">migration: github.com/opencv/opencv_contrib</a>
            </span>
          </td>

我想仅针对<a>标记解析<td class=content>标记之间的值。也就是说,我想从上面的html代码段中只获取.github

我做的如下:

        url = 'https://github.com/opencv/opencv_contrib'
            page = requests.get(url).text

    soup = BeautifulSoup(page, 'html.parser')
    table = soup.find('table', {'class': 'files js-navigation-container js-active-navigation-container'})
    for row in table.findAll("a"):
        print(row.text)

然而,它会在<a> tags之间打印所有内容。如何仅为<td class=content>代码获取该内容?

1 个答案:

答案 0 :(得分:1)

您可以使用单个CSS selector来满足所有要求:

for a in soup.select("td.content a"):
    print(a.get_text())

td.content a会将位于a元素内的所有td元素与content类属性值匹配。

如果您需要单个元素,请使用.select_one()代替.select()