gpt4 book ai didi

python - BeautifulSoup 附加

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

我不明白为什么这不起作用。

soup_main = BeautifulSoup('<html><head></head><body><a>FooBar</a></body></html>')
soup_append = BeautifulSoup('<html><head></head><body><a>Meh</a></body></html>')
soup_main.body.append(soup_append.a)

我收到以下错误:

Traceback (most recent call last):File "<stdin>", line 1, in <module>
File "C:\Python34\lib\site-packages\bs4\element.py", line 378, in append
self.insert(len(self.contents), tag)
File "C:\Python34\lib\site-packages\bs4\element.py", line 312, in insert
raise ValueError("Cannot insert None into a tag.")
ValueError: Cannot insert None into a tag.

如果我能理解正在发生的事情,我会很高兴。

最佳答案

试试这个:

soup_main = BeautifulSoup('<html><head></head><body><a>FooBar</a></body></html>', 'html.parser')
soup_append = BeautifulSoup('<html><head></head><body><a>Meh</a></body></html>', 'html.parser')
soup_main.body.append(soup_append.a)
print(soup_main)

输出:

<html><head></head><body><a>FooBar</a><a>Meh</a></body></html>

希望有帮助。

关于python - BeautifulSoup 附加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50813926/

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