Pylance 正在检测 PageElement 而不是 Tag in find_all -> ResultSet from BeautifulSoup



这是一段代码:

def prepare_security_questions(self, response: requests.Response) -> dict[str, str]:
soup = BeautifulSoup(response.text, 'html.parser')
form: dict[str, Any] = {}
soup_forms = soup.find_all('form')
soup_form = None
for soup_possible_form in soup_forms:
name = soup_possible_form.get('name')

问题是,当我检查soup_possible_form的类型时,Pylance显示它是一个不正确的PageElement对象,它应该是一个Tag对象,并且由于name = soup_possible_form.get('name')被标记为错误,因为根据Pylance消息:

无法访问成员"get">

成员"get">Pylance(reportGeneralTypeIssues)

我正在做一个项目,我大量使用BeautifulSoup,我不想在每个find_all -> ResultSet迭代上使用# type: ignore

好了,我找到了一个可能的解决方案,我检查了每个元素的类型之前所有以下代码:

for soup_possible_form in soup_forms:
if not isinstance(soup_possible_form, Tag):
continue

name = soup_possible_form.get('name')

但我愿意考虑其他解决方案。

最新更新