如何处理 Beautifulsoup 递归错误(或解析错误)
Posted
技术标签:
【中文标题】如何处理 Beautifulsoup 递归错误(或解析错误)【英文标题】:How to deal with Beautifulsoup Recursion Error (or parse error) 【发布时间】:2019-07-28 12:27:47 【问题描述】:我有一堆 html 文件,我想用 Beautifulsoup 来阅读它们。其中一些,我收到了一个错误。我尝试解码,编码......但找不到问题。非常感谢您。
这是一个例子。
import requests
from bs4 import BeautifulSoup
new_text = requests.get('https://www.sec.gov/Archives/edgar/data/1723069/000121390018016357/0001213900-18-016357.txt')
soup = BeautifulSoup(new_text.content.decode('utf-8','ignore').encode("utf-8"),'lxml')
print(soup)
在 Jupyter 笔记本上,出现死内核错误。 在 Pycharm 上,我收到以下错误:(它会重复自己,所以删除了其中一些。但是很长。)
Traceback (most recent call last):
File "C:/Users/oe/.PyCharmCE2019.1/config/scratches/scratch_5.py", line 5, in <module>
print(soup)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1099, in __unicode__
return self.decode()
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\__init__.py", line 566, in decode
indent_level, eventual_encoding, formatter)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1188, in decode
indent_contents, eventual_encoding, formatter)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1257, in decode_contents
formatter))
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1188, in decode
indent_contents, eventual_encoding, formatter)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1257, in decode_contents
formatter))
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1188, in decode
indent_contents, eventual_encoding, formatter)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1257, in decode_contents
formatter))
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1188, in decode
indent_contents, eventual_encoding, formatter)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 1254, in decode_contents
text = c.output_ready(formatter)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 745, in output_ready
output = self.format_string(self, formatter)
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\site-packages\bs4\element.py", line 220, in format_string
if isinstance(formatter, Callable):
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\abc.py", line 190, in __instancecheck__
subclass in cls._abc_negative_cache):
File "C:\Users\oe\Anaconda3\envs\TextAnalysis\lib\_weakrefset.py", line 75, in __contains__
return wr in self.data
RecursionError: maximum recursion depth exceeded in comparison
【问题讨论】:
非常感谢您指出这些。有什么可以让beautifulsoup跳过它们,或者把它们当作文本?我只需要没有标签的文本。所以我的主要目标是摆脱它们。但不要介意他们中的一些人留下来。 这实际上只是发生在print
上。如果你省略了print(soup)
,你应该已经准备好了
如果我称它为其他名称,它也会停止。如果我想搜索它 "new_text = re.search(r'.*?' + r'F-2', str(soup), re.DOTALL).group()" ,它再次进入循环。你认为在 beautifulsoup 之前或之后 prettify() 有帮助吗?
【参考方案1】:
坦率地说,我不确定您的代码的根本问题是什么(尽管我在 Jupyter 笔记本中没有得到死内核),但这似乎可行:
url = 'https://www.sec.gov/Archives/edgar/data/1723069/000121390018016357/0001213900-18-016357.txt'
import requests
from bs4 import BeautifulSoup
new_text = requests.get(url)
soup = BeautifulSoup(new_text.text,'lxml')
print(soup.text)
请注意,在soup
中,new_text.content
被替换为 new_text.text
,我不得不删除编码/解码参数,并且必须将 print
命令从 print(soup)
更改(这引发了错误)到print(soup.text)
工作正常。也许更聪明的人可以解释...
另一个适用的选项是:
import urllib.request
response = urllib.request.urlopen(url)
new_text2 = response.read()
soup = BeautifulSoup(new_text2,'lxml')
print(soup.text)
【讨论】:
以上是关于如何处理 Beautifulsoup 递归错误(或解析错误)的主要内容,如果未能解决你的问题,请参考以下文章