【问题标题】:'NoneType' object is not callable: BeautifulSoup HTML parsing“NoneType”对象不可调用:BeautifulSoup HTML 解析
【发布时间】:2015-07-03 08:38:03
【问题描述】:

我正在尝试解析网页中的 HTML 表格,我将其作为字符串输入传递给 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

我真的无法理解为什么会出现错误,即使&lt;tr&gt; 标记中的某些内容具有相同的id

【问题讨论】:

    标签: python html parsing csv beautifulsoup


    【解决方案1】:

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

    这些对象没有.split() 方法,但它们会尝试搜索它们无法识别的任何属性。 element.split 被翻译为element.find('split'),但没有找到&lt;split&gt; 标签并返回None。由于您使用了element.split(),因此您最终会使用None(),而这会失败。

    您想先从每个元素中提取 文本

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

    【讨论】:

      猜你喜欢
      • 2016-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-06
      • 1970-01-01
      • 2022-07-19
      相关资源
      最近更新 更多