这是一段代码:
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')
但我愿意考虑其他解决方案。