代码之家  ›  专栏  ›  技术社区  ›  Meghan M.

注释掉“for”语句导致“nonetype”没有len()

  •  0
  • Meghan M.  · 技术社区  · 7 年前

    感谢你对我所犯的这个奇怪错误的任何见解。提前谢谢!我仍在努力学习美化组结构以及何时可以使用tag.elements。所以在示例1中,它非常简单,并且如预期的那样出现。在例子2中,我试图写一个 for 洛普,不高兴,就说出来了。

    1)当我再次运行代码时(保存新的更改后),出现以下错误:type error:“nonetype”类型的对象没有len()。奇怪的。有人知道我为什么会看到这个错误吗?我反复检查那些行是否被注释掉,保存,运行。然后删除评论,保存,再次运行。同样。

    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()
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Meghan M.    7 年前

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