gpt4 book ai didi

使用 BeautifulSoup Python None 检查似乎失败

转载 作者:行者123 更新时间:2023-12-01 05:05:31 25 4
gpt4 key购买 nike

我看过类似的帖子,它们与我的情况很接近,但我的结果似乎出乎意料。

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

预先感谢您的帮助!

最佳答案

看起来这只是一个错字。它应该是 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>

关于使用 BeautifulSoup Python None 检查似乎失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25134426/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com