【发布时间】:2014-09-27 20:33:00
【问题描述】:
我看过类似的帖子,与我的情况很接近,但我的结果似乎出乎意料。
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。
提前感谢您的帮助!
【问题讨论】:
-
你的导入不应该是
from bs4 import BeautifulSoup吗?因为BeautifulSoup4在bs4模块中 -
@salmanwahed 是的,我看到其他网站提到 bs4,但是当我安装(通过 easy_install)时,基本库就是 BeautifulSoup。我在其他领域使用它没有问题,所以我怀疑这是 BeautifulSoup 的另一个(更新?)版本?不知道为什么会有差异...
标签: python beautifulsoup nonetype