Ошибка TypeError: cannot serialize (type builtin_function_or_method)
У меня есть такой код
from bs4 import BeautifulSoup
import xml.etree.ElementTree as ET
import requests
def htmlparser(input_file_path, output_file_path):
with open(input_file_path, 'r', encoding='utf-8') as f1:
with open(output_file_path, 'w', encoding='utf-8') as f2:
f2.write('<?xml version="1.0" encoding="UTF-8"?>')
documents = ET.Element('documents')
for url in f1:
document = ET.SubElement(documents, 'document')
document.set('id ', url.index)
url_ = ET.SubElement(document, 'url')
url_.text = url
html = requests.get(url).text
soup = BeautifulSoup(html, 'html.parser')
soup_keywords = BeautifulSoup(html, 'html5lib')
title = soup.find(class_="article__title")
title_ = ET.SubElement(document, 'title')
title_.text = title.string
content = soup.findAll(class_="article__text")
for item in content:
if item.string is not None:
full_text = item.string + ' '
text_ = ET.SubElement(document, 'text')
text_.text = full_text
keywords = soup_keywords.find(attrs={'name': 'keywords'})["content"]
keywords_ = ET.SubElement(document, 'keywords')
keywords_.text = keywords
data = ET.tostring(documents, encoding='utf8')
f2.write(data)
htmlparser('input.csv', 'output.xml')
Каждый раз Python3 выдает ошибку
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1079, in _escape_attrib
if "&" in text:
TypeError: argument of type 'builtin_function_or_method' is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/**/HTMLparser.py", line 45, in <module>
htmlparser('input.csv', 'output.xml')
File "C:/**/HTMLparser.py", line 41, in htmlparser
data = ET.tostring(documents, encoding='utf8')
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1135, in tostring
short_empty_elements=short_empty_elements)
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 776, in write
short_empty_elements=short_empty_elements)
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 941, in _serialize_xml
short_empty_elements=short_empty_elements)
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 933, in _serialize_xml
v = _escape_attrib(v)
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1102, in _escape_attrib
_raise_serialization_error(text)
File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\xml\etree\ElementTree.py", line 1057, in _raise_serialization_error
"cannot serialize %r (type %s)" % (text, type(text).__name__)
TypeError: cannot serialize <built-in method index of str object at 0x0908FA20> (type builtin_function_or_method)
Подскажите, пожалуйста, в чем проблема?