vendredi 17 juin 2016

BeautifulSoup4 - 'NoneType' object has no attribute 'text' but type() returns <class 'bs4.element.Tag'>

Here is my code:

def getdate(game):
    date = game.tbody.tr.td
    month = date.find("span", {"class": "date-month"})
    print(type(month))
    print month.text
    print month.content

The variable game that I am passing in is:

<table class="table non-m" style="-webkit-user-select: auto;">
  <tbody style="-webkit-user-select: auto;">
    <tr rowgameid="R7442744" style="-webkit-user-select: auto;">

      <td style="width: 6%; -webkit-user-select: auto;">
        <span ng-bind-html="trustAsHtml(item.gameDateStr)" ng-if="!isChallenge" style="-webkit-user-select: auto;">
            <div class="schedule-date" style="-webkit-user-select: auto;">
          <span class="date-month" style="-webkit-user-select: auto;">Jul</span>
            <br style="-webkit-user-select: auto;">
          <span class="date-day" style="-webkit-user-select: auto;">04</span>
            </br></div>
        </span>
      </td>

      <td class="text-center" style="width: 15%; -webkit-user-select: auto;">
        <span ng-bind-html="trustAsHtml(item.gameResult)" ng-if="!isChallenge" style="-webkit-user-select: auto;">06:40PM</span>
      </td>

      <td style="width: 30%; -webkit-user-select: auto;">
        <strong style="-webkit-user-select: auto;">@</strong>
        <a href="/spa/team/09367acae6aa46cab1af2806e9730030/home" ng-href="/spa/team/09367acae6aa46cab1af2806e9730030/home" style="-webkit-user-select: auto;"><img class="img-rounded" ng-src="https://s3.amazonaws.com/file.imleagues/Images/Teams/Uploaded/201604/resized/20164290439.jpg_s.jpg" ng-style="{'background-color':initData.schoolFontTitleBack}" src="https://s3.amazonaws.com/file.imleagues/Images/Teams/Uploaded/201604/resized/20164290439.jpg_s.jpg" style="width: 18px; height: 18px; -webkit-user-select: auto; background-color: rgb(5, 94, 170);"> Swift Kick in the Grass</img></a>
      </td>

      <td class="text-center" style="width: 15%; -webkit-user-select: auto;">
        <span class="text-muted" style="-webkit-user-select: auto;">
          <small style="-webkit-user-select: auto;">Outdoor Fields</small>
        </span>
          <br style="-webkit-user-select: auto;">Field 2A</br>
      </td>

      <td class="text-center" ng-bind-html="trustAsHtml(item.mvps)" style="width: 30%; -webkit-user-select: auto;">
        <span class="text-muted" style="-webkit-user-select: auto;">
          <small style="-webkit-user-select: auto;">RSVP:</small>
        </span>
          <br style="-webkit-user-select: auto;">0Y | 0N | 0M</br>
      </td>

    </tr>
  </tbody>
</table>

As you can probably see I am trying to extract the month Jul from the code below. If I print just month variable on it's own I get:

<span class="date-month" style="-webkit-user-select: auto;">Jul</span>

When I try and print month.text or month.content I get the error:

'NoneType' object has no attribute 'text'

This does not make sense to me as when I print type(month) I get:

<class 'bs4.element.Tag'>

What am I missing here? I have used beautifulsoup4 beforehand with code that is very similar to this and I had no problems.

Thanks in advance.

Edit:

Here is my main function

def main():
    soup = BeautifulSoup(open("imlhtml.html"), "html.parser")
    games = soup.findAll("table", {"class": "table non-m"})
    # games = list(games)
    gamedata = []

    for game in games:
        print("############")
        date = getdate(game)
        time = gettime(game)
        opposition = getopposition(game)
        field = getfield(game)
        object = {
            'date': date,
            'time': time,
            'opposition': opposition,
            'field': field,
        }
        gamedata.append(object)

Aucun commentaire:

Enregistrer un commentaire