使用Beautiful Soup时出现“预期的字符串或缓冲区”错误

2024-09-29 22:00:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试一种代码,它将使用Beautiful Soup从URL中提取数字,然后求和这些数字的总和,但我一直得到一个错误,如下所示:

Expected string or buffer

我认为这和正则表达式有关,但我不能指出问题所在。在

import re
import urllib

from BeautifulSoup import *
htm1 = urllib.urlopen('https://pr4e.dr-chuck.com/tsugi/mod/python-data/data/comments_42.html').read()
soup = BeautifulSoup(htm1)
tags = soup('span')

for tag in tags:
    y = re.findall ('([0-9]+)',tag.txt)

print sum(y)

Tags: 代码importreurldatatagtags数字
1条回答
网友
1楼 · 发布于 2024-09-29 22:00:17

我建议使用bs4而不是{}(这是旧版本)。您还需要更改以下行:

y = re.findall ('([0-9]+)',tag)

像这样的事情:

^{pr2}$

看看这是否能让你走得更远:

sum = 0  #initialize the sum
for tag in tags:
    y = re.findall ('([0-9]+)',tag.text)  #get the text from the tag                                                                                                                                    
    print(y[0])  #y is a list, print the first element of the list                                                                                                                                      
    sum += int(y[0])  #convert it to an integer and add it to the sum                                                                                                                                   

print('the sum is: {}'.format(sum))

相关问题 更多 >

    热门问题