使用BeautifulSoup时Python None检查似乎失败

1 投票
1 回答
3169 浏览
提问于 2025-04-18 16:02

我看过一些类似的帖子,虽然它们和我的情况很接近,但我的结果还是让我感到意外。

import BeautifulSoup
import re

soup = BeautifulSoup.BeautifulSoup(<html page of interest>)
if (soup.find_all("td", attrs= {"class": "FilterElement"}, text= re.compile("HERE IS TEXT I AM LOOKING FOR")) is None):
    print('There was no entry')
else:
    print(soup.find("td", attrs= {"class": "FilterElement"}, text= re.compile("HERE IS THE TEXT I AM LOOKING FOR")))

我显然过滤掉了实际的HTML页面,以及正则表达式中的文本。其余部分完全按照我写的内容。然后我遇到了以下错误:

Traceback (most recent call last):
  File "/Users/appa/src/workspace/web_forms/WebForms/src/root/queryForms.py", line 51, in <module>
    LoopThroughDays(form, id, trailer)
  File "/Users/appa/src/workspace/web_forms/WebForms/src/root/queryForms.py", line 33, in LoopThroughDays
    if (soup.find_all("td", attrs= {"class": "FilterElement"}, text= re.compile("HERE IS THE TEXT I AM LOOKING FOR")) is None):
TypeError: 'NoneType' object is not callable

我明白有时候文本可能会缺失。但我以为我设置的这个if语句正好可以捕捉到缺失的情况,因此会返回一个NoneType

提前感谢任何帮助!

1 个回答

2

看起来这只是个打字错误。应该是 soup.findAll 而不是 soup.find_all。我试着运行了一下,改正后就能正常工作了。所以完整的程序应该是:

import BeautifulSoup
import re

soup = BeautifulSoup.BeautifulSoup(<html page of interest>)
if (soup.findAll("td", attrs= {"class": "FilterElement"}, text= re.compile("HERE IS TEXT I AM LOOKING FOR")) is None):
    print('There was no entry')
else:
    print(soup.find("td", attrs= {"class": "FilterElement"}, text= re.compile("HERE IS THE TEXT I AM LOOKING FOR")))<html page of interest>

撰写回答