'NoneType' 对象不可调用:BeautifulSoup HTML 正在解析

'NoneType' object is not callable: BeautifulSoup HTML parsing

我正在尝试解析网页中的 HTML table,我将其作为字符串输入传递给 BeautifulSoup。我给出了以下脚本来解析 HTML 页面并将内容打印在 CSV 文件中:

soup = BeautifulSoup(In_put)
comments = soup.find_all('td', {"id": "TicketDetails_TicketDetail_TicketDetail__ctl0_Tablecell1"})
f = open(Out_put, 'w')
writer = csv.writer(f)
for s in comments:
    writer.writerow(s.split('##'))
f.close()

但它显示错误说:

Traceback (most recent call last):
  File "C:/Users/KOS974/PycharmProjects/test_cases/gasper_try.py", line 561, in <module>
    writer.writerow(s.split('##'))
TypeError: 'NoneType' object is not callable

我真的无法理解为什么会出现错误,即使 <tr> 标签中有一些内容与 id.

相同

您正在尝试在 BeautifulSoup Element 个实例上调用方法 .split()

此类对象 没有 .split() 方法,但它们会尝试搜索它们无法识别的任何属性。 element.split 被翻译成 element.find('split'),但是没有找到 <split> 标签,返回 None。由于您使用了 element.split(),因此您最终使用的是 None(),但失败了。

您想先从每个元素中提取 text:

for s in comments:
    writer.writerow(s.get_text().split('##'))