我想获取嵌套标签中的数字。我该怎么做?
我的代码输出了这个,但我想得到#40,而不是整两行:
<span class="rankings-score">
<span>#40</span>
这是我的代码:
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']
我收到此错误:
AttributeError: 'NoneType' object has no attribute 'span'
当我尝试这个时:
d['national_rank'] = row.find("span", 'rankings-score').span.text