在Beautifulsoup提取价值 [英] Extracting value in Beautifulsoup

查看:162
本文介绍了在Beautifulsoup提取价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code:

f = open(path, 'r')
html = f.read() # no parameters => reads to eof and returns string

soup = BeautifulSoup(html)
schoolname = soup.findAll(attrs={'id':'ctl00_ContentPlaceHolder1_SchoolProfileUserControl_SchoolHeaderLabel'})
print schoolname

这给:

[<span id="ctl00_ContentPlaceHolder1_SchoolProfileUserControl_SchoolHeaderLabel">A B Paterson College, Arundel, QLD</span>]

当我尝试用 schoolname(AB帕特森学院,阿伦德尔,QLD [价值'] 我收到以下错误访问值IE)':

when I try and access the value (i.e. 'A B Paterson College, Arundel, QLD) by using schoolname['value'] I get the following error:

print schoolname['value'] TypeError: list indices must be integers, not str

我是什么做错了获得价值?

What am I doing wrong to get that value?

推荐答案

您可以使用的 内容 下移树:

You can use contents to move down the tree:

>>> for x in schoolname:
>>>    print x.contents
[u'A B Paterson College, Arundel, QLD']    

请注意,该内容不一定必须是一个串 - 一般也可以是更多的标签或串和标记的混合物

Note that the contents doesn't necessarily have to be a string - in general it could also be more tags or a mixture of string and tags.

这篇关于在Beautifulsoup提取价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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