【发布时间】:2023-03-24 21:30:02
【问题描述】:
我有以下 HTML 结构:
<BODY><tag1></tag1><tag2></tag2></BODY>
我需要使用 BeautifulSoup 在</BODY> 之前插入一个表格。
我现在拥有的是:
re.sub(r'\s/BODY\s', '<Table>Test<Table></BODY>', BeautifulSoup(report, "lxml"))
我得到的错误是:
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
execfile("C:\Users\My_PC\Desktop\Report.py")
File "C:\Users\My_PC\Desktop\Report.py", line 10, in <module>
re.sub(r'\s/BODY\s', '<Table>Test<Table></BODY>', BeautifulSoup(report, "lxml"))
File "C:\Python27\lib\re.py", line 155, in sub
return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer
任何帮助将不胜感激。
【问题讨论】:
-
re.sub() 需要字符串作为最后一个参数,但你已经给了 BeautifulSoup 对象。
标签: python html insert tags beautifulsoup