加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – BeautifulSoup.find的返回值是多少?

发布时间:2020-12-14 06:03:51 所属栏目:百科 来源:网络整理
导读:我跑去获得一些分数值. score = soup.find('div',attrs={'class' : 'summarycount'}) 我运行’打印分数’得到如下. div class="summarycount"524/div 我需要提取数字部分.我使用了re模块但是失败了. m = re.search("[^d]+(d+)",score) TypeError: expect
我跑去获得一些分数值.

score = soup.find('div',attrs={'class' : 'summarycount'})

我运行’打印分数’得到如下.

<div class="summarycount">524</div>

我需要提取数字部分.我使用了re模块但是失败了.

m = re.search("[^d]+(d+)",score)
TypeError: expected string or buffer

function search in re.py at line 142
return _compile(pattern,flags).search(string)

>查找功能的返回类型是什么?
>如何从得分变量中获取数字?
>有没有简单的方法让BeautifulSoup返回值(在本例中为524)本身?

解决方法

它返回一个对象,您可以使用该对象进行进一步搜索或使用score.contents提取其内容:

from BeautifulSoup import BeautifulSoup

str = r'''
    <body>
    <div class="summarycount">524</div>
    <div class="foo">111</div>
    </body>
'''

soup = BeautifulSoup(str)
score = soup.find('div',attrs={'class' : 'summarycount'})

print type(score)
print score.contents

打印:

<class 'BeautifulSoup.Tag'>
[u'524']

包含多个示例的完整文档是available here.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读