如何使用beautifulSoup访问span?

gol*_*ine 2 python beautifulsoup

我想在嵌套标签中获取数字.我该怎么做?

我的代码输出了这个,但我想得到#40,而不是整行两行:

<span class="rankings-score">
<span>#40</span>
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

from bs4 import BeautifulSoup
import requests
import csv

site =  "http://www.usnews.com/education/best-high-schools/national-rankings/page+2"

fields = ['national_rank','school','address','school_page','medal','ratio','size_desc','students','teachers'] 

r = requests.get(site)
html_source = r.text
soup = BeautifulSoup(html_source)

table = soup.find('table')    
rows_list = []      

for row in table.find_all('tr'):                                                                                                                                                                                                                                               

    d = dict()

    d['national_rank'] = row.find("span", 'rankings-score')
    print d['national_rank']
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

AttributeError: 'NoneType' object has no attribute 'span'
Run Code Online (Sandbox Code Playgroud)

当我尝试这个:

d['national_rank'] = row.find("span", 'rankings-score').span.text
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 5

访问嵌套范围的文本:

score_span = row.find("span", 'rankings-score')
if score_span is not None:
    print score_span.span.text
Run Code Online (Sandbox Code Playgroud)

你需要确保row.find("span", 'rankings-score')实际找到的东西; 上面我测试存在确实是一个<span>发现.

如果没有找到匹配的对象,则该.find()方法返回None,因此通常,每当您收到AttributeError: 'NoneType' object has no attribute ...涉及您尝试加载的对象的异常时,Element.find()您需要None 尝试进一步访问信息之前进行测试.

这适用于object.find,object.find_all,object[...]标签属性访问object.<tagname>,object.select等等等等.