简体   繁体   English

如何使用beautifulSoup访问span?

[英]How to access span using beautifulSoup?

I would like to get the number within the nested tag. 我想在嵌套标签中获取数字。 How would I do this? 我该怎么做?

My code outputs this, but I'd like to get the #40, not the whole two lines: 我的代码输出了这个,但是我想得到#40,而不是整个两行:

<span class="rankings-score">
<span>#40</span>

Here is my code: 这是我的代码:

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']

I get this error: 我收到此错误:

AttributeError: 'NoneType' object has no attribute 'span'

when I try this: 当我尝试这个:

d['national_rank'] = row.find("span", 'rankings-score').span.text

access the text of the nested span: 访问嵌套范围的文本:

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

You need to make sure that row.find("span", 'rankings-score') actually found something; 您需要确保row.find("span", 'rankings-score')实际上找到了一些东西; above I test that there is indeed a <span> found. 以上我测试是否确实找到了<span>

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. 如果未找到匹配的对象,则.find()方法将返回None ,因此通常,每当您遇到AttributeError: 'NoneType' object has no attribute ...异常,其中涉及您尝试使用Element.find()加载的对象,那么你需要测试None试图进一步获取信息之前

This applies to object.find , object.find_all , object[...] tag attribute access, object.<tagname> , object.select , etc. etc. 这适用于object.findobject.find_allobject[...]标签属性访问, object.<tagname>object.select等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM