2017-07-12 161 views
1

之間的html文件,我有一個HTML文件,如下所示:如何解析特定TD標籤

<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> 

我只想爲<td class=content>標籤解析<a>標籤之間的值。也就是說,我只想從上面的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

可以滿足所有的要求與單個CSS selector

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

td.content a將匹配位於td元件內的所有a元件與具有content類屬性值。

如果您需要單個元素,請使用.select_one()而不是.select()