如何使用BeautifulSoup访问跨度? [英] How to access span using beautifulSoup?

查看:434
本文介绍了如何使用BeautifulSoup访问跨度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to get the number within the nested tag. How would I do this?

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

My code outputs this, but I'd like to get the #40, not the whole two lines:

<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

推荐答案

访问嵌套范围的文本:

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

您需要确保row.find("span", 'rankings-score')实际找到了东西;上面我测试了是否确实找到了 .

You need to make sure that row.find("span", 'rankings-score') actually found something; above I test that there is indeed a <span> found.

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

The .find() method returns None if no matching object was found, so in general, whenever you get a AttributeError: 'NoneType' object has no attribute ... exception, involving an object you tried to load with Element.find(), then you need to test for None before trying to further access information.

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

This applies to object.find, object.find_all, object[...] tag attribute access, object.<tagname>, object.select, etc. etc.

这篇关于如何使用BeautifulSoup访问跨度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆