【问题标题】:Python: BeautifulSoup UnboundLocalErrorPython:BeautifulSoup UnboundLocalError
【发布时间】:2019-04-07 07:44:12
【问题描述】:

我正在尝试从一些 .txt 格式的文档中删除 HTML 标记。但是,据我了解,bs4 似乎存在错误。我得到的错误如下:

 Traceback (most recent call last):
  File "E:/Google Drive1/Thesis stuff/Python/database/get_missing_10ks.py", line 13, in <module>
    text = BeautifulSoup(file_read, "html.parser")
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\site-packages\bs4\__init__.py", line 282, in __init__
    self._feed()
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\site-packages\bs4\__init__.py", line 343, in _feed
    self.builder.feed(self.markup)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\site-packages\bs4\builder\_htmlparser.py", line 247, in feed
    parser.feed(markup)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\html\parser.py", line 111, in feed
    self.goahead(0)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\html\parser.py", line 179, in goahead
    k = self.parse_html_declaration(i)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\html\parser.py", line 264, in parse_html_declaration
    return self.parse_marked_section(i)
  File "C:\Users\Adrian PC\AppData\Local\Programs\Python\Python37\lib\_markupbase.py", line 160, in parse_marked_section
    if not match:
UnboundLocalError: local variable 'match' referenced before assignment

我使用的代码如下:

import os
from bs4 import BeautifulSoup

path_to_10k = "D:/10ks/list_missing_10k/"

path_to_saved_10k = "D:/10ks/list_missing_10kp/"

list_txt = os.listdir(path_to_10k)

for name in list_txt:
    file = open(path_to_10k + name, "r+", encoding="utf-8")
    file_read = file.read()
    text = BeautifulSoup(file_read, "html.parser")
    text = text.get_text("\n")
    file2 = open(path_to_saved_10k + name, "w+", encoding="utf-8")
    file2.write(str(text))
    file2.close()
    file.close()

问题是我已经在 51320 个文档上使用了这个方法,它工作得很好,但是,有一些文档它不能做。当我打开这些 HTML 文档时,它们对我来说似乎是一样的。如果有人能指出可能是什么问题以及如何解决它,那就太好了。谢谢!

文件示例:https://files.fm/u/2s45uafp

【问题讨论】:

  • 看起来像一个错误。可能值得将其缩小到可重复的测试用例(捕获导致此错误的任何数据)并在 github 上提出。
  • 不,目前没有。这是 CPython 中的一个需要修复的错误,但我之前没有在错误跟踪器上提出问题,所以我想确定导致它的情况。我在聊天中提出了它;不错的收获:)
  • 您能否提供一个示例文件,其中会出现问题?
  • @AndrasDeak 是的。我上传了一个文件,该文件在以下位置中断:files.fm/u/2s45uafp
  • 看起来类似于bugs.python.org/issue34480

标签: python html parsing beautifulsoup text-files


【解决方案1】:

https://github.com/scrapy/w3lib
https://w3lib.readthedocs.io/en/latest/

pip install w3lib

from w3lib.html import remove_tags

然后remove_tags(data)返回清除数据。

【讨论】:

    【解决方案2】:

    这是一个使用正则表达式删除 HTML 标记的解决方案。

    import re
    
    TAG_RE = re.compile(r'<[^>]+>')
    
    f = open("C:\Temp\Data.txt", "r")
    strHtml=f.read()
    
    def remove_Htmltags(text):
        return TAG_RE.sub('', text)
    
    strClearText=remove_Htmltags(strHtml)
    print(strClearText)
    

    【讨论】:

      猜你喜欢
      • 2017-11-27
      • 2019-05-27
      • 2014-09-16
      • 1970-01-01
      • 2013-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-14
      相关资源
      最近更新 更多