【问题标题】:AttributeError: ' ' object has no attribute 'soup'AttributeError:“”对象没有属性“汤”
【发布时间】:2017-08-13 12:21:40
【问题描述】:

当我调用以下函数时

def get_words(self):
    blocks = self.soup.find_all("block", {"blockType": lambda x: x not in ('Separator', 'SeparatorsBox')})

    wrds_blcks = []

    for i, block in enumerate(blocks):
        if block['blockType'] == 'Table':
            rslt = self._get_words_from_block_table(block)

        else:
            rslt = self._get_words_from_block_text(block)

        rslt = self._cleanup_word(rslt)
        if rslt != [[]] and rslt != []:
            wrds_blcks.append(rslt)

    return wrds_blcks

我得到以下错误

 in get_words
    blocks = self.soup.find_all("block", {"blockType": lambda x: x not in ('Separator', 'SeparatorsBox')})
AttributeError: 'AbbyExtractor' object has no attribute 'soup'

参考第一行:

 blocks = self.soup.find_all("block", {"blockType": lambda x: x not in ('Separator', 'SeparatorsBox')})

怎么了?

【问题讨论】:

    标签: beautifulsoup python-3.5 bs4


    【解决方案1】:

    您需要先创建soup。将从网页检索到的html 代码作为参数传递给get_words 方法。并制作soup。然后做你的任务。

    def get_words(self, html):
    
        self.soup = BeautifulSoup(html,"lxml")
        blocks = self.soup.find_all("block", {"blockType": lambda x: x not in ('Separator', 'SeparatorsBox')})
    
        wrds_blcks = []
    
        for i, block in enumerate(blocks):
            if block['blockType'] == 'Table':
                rslt = self._get_words_from_block_table(block)
    
            else:
                rslt = self._get_words_from_block_text(block)
    
            rslt = self._cleanup_word(rslt)
            if rslt != [[]] and rslt != []:
                wrds_blcks.append(rslt)
    
        return wrds_blcks
    

    【讨论】:

    • @vincent 如果是xml 文件,则将html.parser 替换为lxml
    猜你喜欢
    • 1970-01-01
    • 2012-12-01
    • 2021-04-19
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多