【问题标题】:table.decompose(): AttributeError: 'str' object has no attribute 'decompose'table.decompose(): AttributeError: 'str' object has no attribute 'decompose'
【发布时间】:2020-04-26 09:51:48
【问题描述】:

我正在尝试使用 BeautifulSoup 来解析 html 文档。我试图编写一个可以解析文档的代码,找到所有表并删除那些有 数字/字母数字比率 > 15%。我已使用给出的代码作为上一个问题的答案:

Delete HTML element if it contains a certain amount of numeric characters

但由于某种原因,table.decompose() 参数被标记为错误。我会很感激我能得到的任何帮助。请注意,我是初学者,因此,虽然我尝试过,但我并不总是理解更复杂的解决方案!

代码如下:

test_file = 'locationoftestfile.html'


# Define a function to remove tables which have numeric characters/ alphabetic and numeric characters > 15%
def remove_table(table):
        table = re.sub('<[^>]*>', ' ', str(table))
        numeric = sum(c.isdigit() for c in table)
        print('numeric: ' + str(numeric))
        alphabetic = sum(c.isalpha() for c in table)
        print('alpha: ' + str(alphabetic))
        try:
                ratio = numeric / float(numeric + alphabetic)
                print('ratio: '+ str(ratio))
        except ZeroDivisionError as err:
                ratio = 1
        if ratio > 0.15: 
            table.decompose()


# Define a function to create our Soup object and then extract text
def file_to_text(file):
    soup_file = open(file, 'r')
    soup = BeautifulSoup(soup_file, 'html.parser')
    for table in soup.find_all('table'):
        remove_table(table)
    text = soup.get_text()
    return text


file_to_text(test_file)

这是我收到的输出/错误:

numeric: 1
alpha: 55
ratio: 0.017857142857142856
numeric: 9
alpha: 88
ratio: 0.09278350515463918
numeric: 20
alpha: 84
ratio: 0.19230769230769232
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-28-c7e380df4fdc> in <module>
----> 1 file_to_text(test_file)

<ipython-input-27-9fb65cec1313> in file_to_text(file)
     16                 ratio = 1
     17         if ratio > 0.15:
---> 18             table.decompose()
     19     text = soup.get_text()
     20     return text

AttributeError: 'str' object has no attribute 'decompose'

请注意table.decompose() 参数与我链接的解决方案中给出的参数不同。该解决方案使用

   return True
else:
   return False

但是,也许天真地,我不明白这会如何删除表格。

【问题讨论】:

  • 那段代码对我来说看起来很 "wild"(用正则表达式解析 html)。您可以分享 HTML 还是可以编辑问题并将示例(小)输入和预期输出放在那里?
  • 我同意@AndrejKesely 的观点,table.decompose() 错误可能是您遇到的最小问题。
  • 你们可能都非常非常正确。此代码是我的讲师(第二个定义)提供的代码和我从链接中获取的代码的混搭。值得庆幸的是,下面的解决方案似乎已经奏效了,所以,就目前而言,这对我有用!

标签: python beautifulsoup attributeerror


【解决方案1】:
table = re.sub('<[^>]*>', ' ', str(table))

这会用字符串覆盖参数“table”。您可能想在此处为变量使用另一个名称。例如

def remove_table(table):
    table_as_str = re.sub('<[^>]*>', ' ', str(table))
    numeric = sum(c.isdigit() for c in table_as_str)
    print('numeric: ' + str(numeric))
    alphabetic = sum(c.isalpha() for c in table_as_str)
    print('alpha: ' + str(alphabetic))
    try:
            ratio = numeric / float(numeric + alphabetic)
            print('ratio: '+ str(ratio))
    except ZeroDivisionError as err:
            ratio = 1
    if ratio > 0.15: 
        table.decompose()

【讨论】:

    猜你喜欢
    • 2017-05-10
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2021-09-29
    相关资源
    最近更新 更多