【发布时间】:2026-02-22 11:10:01
【问题描述】:
我编写了以下代码来提取所有编号。从网页并添加所有这些。但我想在不使用正则表达式的情况下对其进行编码,所以请指导我如何去做。 链接:http://python-data.dr-chuck.net/comments_361585.html
我的代码:
import urllib
import re
from BeautifulSoup import *
html = urllib.urlopen('http://python-data.dr-chuck.net/comments_361585.html ').read()
soup = BeautifulSoup(html)
# Retrieve all of the anchor tags
tags = soup('td')
total = 0
for tag in tags:
# Look at the parts of a tag
line = str(tag)
x = re.findall('[0-9]+',line)
if len(x) > 0:
for item in x:
total += int(item)
print(total)
不使用正则表达式我试过这个:
import urllib
from BeautifulSoup import *
url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
tags = soup.find_all('span', text=True)
for tag in tags:
number=tag.get('class', None)
total = sum( int(tag.text) for tag in tags )
print ('total')
但它有一些错误:“NoneType”对象不可调用。
指导我如何修复它。
【问题讨论】:
-
你应该做你自己的功课......
-
我已经这样做了..我想知道第二种方式
标签: python web beautifulsoup numbers extract