TypeError: 'NoneType' object is not callable (Python: Scraping from HTML data)
TypeError: 'NoneType' object is not callable (Python: Scraping from HTML data)
我正在尝试从 HTML 数据中抓取数字,以便获得它们的总和。但是,当我尝试 运行 时,我 运行 遇到了上述错误。它指的是 "data = " 行。这行代码中的这个错误指的是什么?我是否正确设置了 "for" 循环?谢谢你的想法。
import urllib
from bs4 import BeautifulSoup
url = "http://python-data.dr-chuck.net/comments_42.html"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
tags = soup('span')
data = soup.findall("span", {"Comments":"Comments"})
numbers = [d.text for d in data]
summation = 0
for tag in tags:
print tags
y= tag.finall("span").text
summation = summation + int(y)
print summation
这是 HTML 数据的样子:
<tr><td>Modu</td><td><span class="comments">90</span></td></tr>
<tr><td>Kenzie</td><td><span class="comments">88</span></td></tr>
<tr><td>Hubert</td><td><span class="comments">87</span></td></tr>
首先,BeautifulSoup
中没有findall()
方法——有find_all()
。此外,您基本上是在搜索具有 Comments
属性且具有 Comments
值的元素:
soup.findall("span", {"Comments":"Comments"})
而且,这是 Python,你可以用 built-in sum()
.
来总结更容易
固定版本:
data = soup.find_all("span", {"class": "comments"})
print sum(int(d.text) for d in data) # prints 2482
我正在尝试从 HTML 数据中抓取数字,以便获得它们的总和。但是,当我尝试 运行 时,我 运行 遇到了上述错误。它指的是 "data = " 行。这行代码中的这个错误指的是什么?我是否正确设置了 "for" 循环?谢谢你的想法。
import urllib
from bs4 import BeautifulSoup
url = "http://python-data.dr-chuck.net/comments_42.html"
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
tags = soup('span')
data = soup.findall("span", {"Comments":"Comments"})
numbers = [d.text for d in data]
summation = 0
for tag in tags:
print tags
y= tag.finall("span").text
summation = summation + int(y)
print summation
这是 HTML 数据的样子:
<tr><td>Modu</td><td><span class="comments">90</span></td></tr>
<tr><td>Kenzie</td><td><span class="comments">88</span></td></tr>
<tr><td>Hubert</td><td><span class="comments">87</span></td></tr>
首先,BeautifulSoup
中没有findall()
方法——有find_all()
。此外,您基本上是在搜索具有 Comments
属性且具有 Comments
值的元素:
soup.findall("span", {"Comments":"Comments"})
而且,这是 Python,你可以用 built-in sum()
.
固定版本:
data = soup.find_all("span", {"class": "comments"})
print sum(int(d.text) for d in data) # prints 2482