我如何 运行 BeaufiulSoup 3 中的 BeaufiulSoup4 命令?

How do I run a BeaufiulSoup4 command in BeauifulSoup 3?

我正在尝试向更大的项目添加新代码,但我的代码使用 BeauifulSoup4,而项目最初使用 BeautifulSoup 3.2.1。如果我尝试在我的代码上使用 运行 BeautifulSoup 3.2.1,我的代码中存在的 .parent 命令会出错。我尝试将项目升级到 BeauifulSoup4,但是在我不想篡改的项目的一部分中,我从失败的测试中得到了错误。我的主管告诉我,安装同一个 pip 的两个不同版本是违反公司政策的。有没有办法将 .parent 命令添加到 BeautifulSoup 3.2.1?

soup = BeautifulSoup(response.content, 'html.parser') na = soup.find(id='pizza stores') links = na.parent.parent.find_all('a')

当我尝试 运行 BeautifulSoup 3 中的这段代码时,我得到

TypeError: 'NoneType' object is not callable

我变了变了 BeautifulSoup(response.content, 'html.parser')BeautifulSoup(response.content) 在 BeautifulSoup 中尝试 运行 3.

问题不在parent,在find_all,应该是findAll在bs3。

doc = '<html><body><div><div id="pizza stores"></div></div><a>link</a></body></html>'
sp = BeautifulSoup.BeautifulSoup(doc)
na = sp.find(id='pizza stores')
na.parent.parent.findAll('a')

Out[44]: [<a>link</a>]