問題描述
我正在嘗試讓解析器使用 beautifulSoup 和多處理.我有一個錯誤:
I'm trying to make a parser use beautifulSoup and multiprocessing. I have an error:
RecursionError: 超出最大遞歸深度
RecursionError: maximum recursion depth exceeded
我的代碼是:
import bs4, requests, time
from multiprocessing.pool import Pool
html = requests.get('https://www.avito.ru/moskva/avtomobili/bmw/x6?sgtd=5&radius=0')
soup = bs4.BeautifulSoup(html.text, "html.parser")
divList = soup.find_all("div", {'class': 'item_table-header'})
def new_check():
with Pool() as pool:
pool.map(get_info, divList)
def get_info(each):
pass
if __name__ == '__main__':
new_check()
為什么會出現此錯誤以及如何解決?
Why I get this error and how I can fix it?
更新:所有錯誤文本都是
Traceback (most recent call last):
File "C:/Users/eugen/PycharmProjects/avito/main.py", line 73, in <module> new_check()
File "C:/Users/eugen/PycharmProjects/avito/main.py", line 67, in new_check
pool.map(get_info, divList)
File "C:UserseugenAppDataLocalProgramsPythonPython36libmultiprocessingpool.py", line 266, in map
return self._map_async(func, iterable, mapstar, chunksize).get()
File "C:UserseugenAppDataLocalProgramsPythonPython36libmultiprocessingpool.py", line 644, in get
raise self._value
File "C:UserseugenAppDataLocalProgramsPythonPython36libmultiprocessingpool.py", line 424, in _handle_tasks
put(task)
File "C:UserseugenAppDataLocalProgramsPythonPython36libmultiprocessingconnection.py", line 206, in send
self._send_bytes(_ForkingPickler.dumps(obj))
File "C:UserseugenAppDataLocalProgramsPythonPython36libmultiprocessing
eduction.py", line 51, in dumps
cls(buf, protocol).dump(obj)
RecursionError: maximum recursion depth exceeded
推薦答案
當你使用 multiprocessing
時,你傳遞給 worker 的所有東西都必須是 腌制.
When you use multiprocessing
, everything you pass to a worker has to be pickled.
很遺憾,很多 BeautifulSoup
樹無法腌制.
Unfortunately, many BeautifulSoup
trees can't be pickled.
這有幾個不同的原因.其中一些是已修復的錯誤,因此您可以嘗試確保您擁有最新的 bs4 版本,而有些則特定于不同的解析器或樹構建器......但很有可能沒有這樣的事情會有幫助的.
There are a few different reasons for this. Some of them are bugs that have since been fixed, so you could try making sure you have the latest bs4 version, and some are specific to different parsers or tree builders… but there's a good chance nothing like this will help.
但根本問題是樹中的許多元素都包含對樹其余部分的引用.
But the fundamental problem is that many elements in the tree contain references to the rest of the tree.
有時,這會導致實際的無限循環,因為循環引用對于其循環引用檢測來說過于間接.但這通常是一個可以修復的錯誤.
Occasionally, this leads to an actual infinite loop, because the circular references are too indirect for its circular reference detection. But that's usually a bug that gets fixed.
但是,更重要的是,即使循環不是無限,它仍然可以從樹的其余部分拖入 1000 多個元素,這已經足以導致遞歸錯誤
.
But, even more importantly, even when the loop isn't infinite, it can still drag in more than 1000 elements from all over the rest of the tree, and that's already enough to cause a RecursionError
.
我認為后者就是這里發生的事情.如果我使用您的代碼并嘗試腌制 divList[0]
,它會失敗.(如果我提高遞歸限制并計算幀數,它需要 23080 的深度,這遠遠超過默認的 1000.)但是如果我采用完全相同的 div
并解析分開,它成功沒有問題.
And I think the latter is what's happening here. If I take your code and try to pickle divList[0]
, it fails. (If I bump the recursion limit way up and count the frames, it needs a depth of 23080, which is way, way past the default of 1000.) But if I take that exact same div
and parse it separately, it succeeds with no problem.
所以,一種可能性是只做 sys.setrecursionlimit(25000)
.這將解決這個確切頁面的問題,但是稍微不同的頁面可能需要更多.(另外,將遞歸限制設置得那么高通常不是一個好主意——不是因為浪費了內存,而是因為這意味著實際的無限遞歸需要 25 倍的時間和 25 倍的資源浪費來檢測.)
So, one possibility is to just do sys.setrecursionlimit(25000)
. That will solve the problem for this exact page, but a slightly different page might need even more than that. (Plus, it's usually not a great idea to set the recursion limit that high—not so much because of the wasted memory, but because it means actual infinite recursion takes 25x as long, and 25x as much wasted resources, to detect.)
另一個技巧是編寫修剪樹"的代碼,在您腌制之前/之后消除 div 中的任何向上鏈接.這是一個很好的解決方案,但它可能需要大量工作,并且需要深入了解 BeautifulSoup 的工作原理,我懷疑你是否愿意這樣做.
Another trick is to write code that "prunes the tree", eliminating any upward links from the div before/as you pickle it. This is a great solution, except that it might be a lot of work, and requires diving into the internals of how BeautifulSoup works, which I doubt you want to do.
最簡單的解決方法有點笨拙,但是……您可以將湯轉換為字符串,將其傳遞給孩子,然后讓孩子重新解析它:
The easiest workaround is a bit clunky, but… you can convert the soup to a string, pass that to the child, and have the child re-parse it:
def new_check():
divTexts = [str(div) for div in divList]
with Pool() as pool:
pool.map(get_info, divTexts)
def get_info(each):
div = BeautifulSoup(each, 'html.parser')
if __name__ == '__main__':
new_check()
這樣做的性能成本可能無關緊要;更大的擔憂是,如果您的 HTML 不完美,則轉換為字符串并重新解析它可能不是完美的往返.所以,我建議你先做一些沒有多處理的測試,以確保這不會影響結果.
The performance cost for doing this is probably not going to matter; the bigger worry is that if you had imperfect HTML, converting to a string and re-parsing it might not be a perfect round trip. So, I'd suggest that you do some tests without multiprocessing first to make sure this doesn't affect the results.
這篇關于超出最大遞歸深度.多處理和 bs4的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!