这是我的代码:
def getdate(game):
date = game.tbody.tr.td
month = date.find("span", {"class": "date-month"})
print(type(month))
print month.text
print month.content
我传递的变量game
是:
<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>
您可能会看到我正在尝试从下面的代码中提取月份 Jul 。如果我自己打印month
变量,我会得到:
<span class="date-month" style="-webkit-user-select: auto;">Jul</span>
当我尝试打印month.text
或month.content
时,我收到了错误消息:
'NoneType' object has no attribute 'text'
这对我来说没有意义,因为当我打印type(month)
时,我得到了:
<class 'bs4.element.Tag'>
我在这里缺少什么?我事先使用了beautifulsoup4,代码与此非常相似,我没有遇到任何问题。
提前致谢。
修改
这是我的主要功能
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)