BS4 python添加标签错误

时间:2015-07-02 12:23:22

标签: python beautifulsoup

我正在尝试使用BS4在我的标签下的新行上添加一个标记,但是出现错误

from bs4 import BeautifulSoup as Soup


soup = Soup(open("filter2.html"))

head = soup.find('HEAD')
meta = soup.new_tag('META')
meta['content'] = "text/html; charset=UTF-8"
meta['http-equiv'] = "Content-Type"
title.insert_after(meta)

print soup

这里是python代码

<HTML>
<HEAD>
<META NAME="robots" CONTENT="none">
<LINK REL="stylesheet" HREF="a.css" TYPE="text/css">
</HEAD>

和我的HTML文件

ToolTip.Enable

我不确定是什么问题。我看看文档,一切似乎都是正确的。 任何想法?

没有TITLE标签,它不需要一个作为其网页,不会被许多人用于我自己和家人

2 个答案:

答案 0 :(得分:0)

Beautiful Soup会自动猜测无效的HTML标记并将其更改为有效的HTML标记。从而将<HEAD>更改为<head>

soup = Soup(open("filter2.html"))

head = soup.find('head')
title = soup.new_tag('title')
title.insert(1, "Some Title")
head.insert(1, title)

我建议您正确使用HTML标记以避免头痛。

答案 1 :(得分:0)

解决了它。经过一段时间的谷歌和一杯咖啡之后

现已完成

from bs4 import BeautifulSoup as Soup

soup = Soup(open("filter2.html"))

head = soup.find('head')
metatag = soup.new_tag('meta')
metatag.attrs['http-equiv'] = 'Content-Type'
metatag.attrs['content'] = 'text/html'
soup.head.append(metatag)

html = soup.prettify("utf-8")
with open("filter2.html", "wb") as file:
    file.write(html)