line = line.strip() TypeError: 'NoneType' 对象不可调用 [英] line = line.strip() TypeError: 'NoneType' object is not callable
问题描述
我正在尝试使用 beautifulsoup
从 html 的列表中查找所有 num:
I am trying to find all num's in a list from an html using beautifulsoup
:
import urllib
from BeautifulSoup import *
import re
line = None
url = raw_input('Enter - ')
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html)
# Retrieve all of the anchor tags
tags = soup('span')
for line in tags:
line = line.strip()
numlist = re.findall('[0-9]+' , tags)
print numlist`
我得到一个回溯:
回溯(最近一次调用最后一次):文件C:\Documents andSettings\mea388\Desktop\PythonSchool\new 12.py",第 14 行,在line = line.strip() TypeError: 'NoneType' 对象不可调用
Traceback (most recent call last): File "C:\Documents and Settings\mea388\Desktop\PythonSchool\new 12.py", line 14, in line = line.strip() TypeError: 'NoneType' object is not callable
我不明白为什么我会得到回溯.
I cannot understand why I'm getting a traceback.
推荐答案
那是因为您正试图在 beautiful Soup 中的标签类上运行 strip.
That's because you are trying to run strip on the tag class within beautiful soup.
将第 14 行更改为:
Change line 14 to:
line = line.string.strip()
但是请注意,当您搜索的标签具有多个子元素时,这仍然可以是 None.请参阅 链接到 doco 上的字符串方法以获得美丽的汤
However be aware that this can still be None when the tag you are searching for has multiple sub elements. Seee link to string method on doco for beautiful soup
这篇关于line = line.strip() TypeError: 'NoneType' 对象不可调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!