【问题标题】:Commented out "for" statement results in 'NoneType' has no len()注释掉“for”语句导致 'NoneType' 没有 len()
【发布时间】:2018-08-15 17:19:58
【问题描述】:

感谢您对我遇到的这个奇怪错误的任何见解。提前致谢!我仍在尝试学习 BeautifulSoup 结构以及 Tag.Elements 何时可用。因此,在示例 1 中,它非常简单并且按预期出现。在示例 2 中,我试图编写一个 for 循环,但不满意并将其注释掉。

1) 当我再次运行代码时(保存新更改后),我收到此错误:TypeError:'NoneType' 类型的对象没有 len()。奇怪的。任何人都知道为什么我可能会看到这个错误?我仔细检查了这些行是否被注释掉、保存、运行。然后删除cmets,保存,再次运行。相同的。

EXAMPLE 1:
chapter = soup.find('h3', text=re.compile('chapter', re.IGNORECASE))
print(type(chapter))
print(chapter.string)
p_tag = 'p'
item = chapter
print(item)
print(type(item))

=== RESTART: D:\Scripts\Python\batch content editing\get_tree_position.py ===
<class 'bs4.element.Tag'>
CHAPTER ONE 
CHAPTER ONE
<class 'bs4.element.Tag'>


EXAMPLE 2:
chapter = soup.find('h3', text=re.compile('chapter', re.IGNORECASE))
print(type(chapter))
print(chapter.string)
#print(type(soup.chapter))
p_tag = 'p'
item = chapter
print(item.string)
print(type(item)) 
#for item in h3.findNextSiblings():
#   getattr(item, 'name')

=== RESTART: D:\Scripts\Python\batch content editing\get_tree_position.py ===
Traceback (most recent call last):
  File "D:\Scripts\Python\batch content editing\get_tree_position.py", line 28, in <module>
    big_soup = BeautifulSoup(contents, 'html.parser')
  File "C:\Python\Python36\lib\site-packages\bs4\__init__.py", line 246, in __init__
    elif len(markup) <= 256 and (
TypeError: object of type 'NoneType' has no len()

【问题讨论】:

  • cmets 不是错误的原因。 Traceback 显示您的错误发生在第 28 行,但您没有在问题中包含该代码。

标签: python types beautifulsoup tags


【解决方案1】:

抱歉,这是一个完全不同的部分中的编辑器或鼠标垫错误。我在 berklem 发布的同时发现了它。所以谢谢你,伯克伦!并道歉...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多