使用BeautifulSoup添加元标记

时间:2014-04-16 07:10:32

标签: python python-2.7 beautifulsoup

如何使用Beautiful Soup(库)在HTML页面中的标题标记之后添加元标记。我使用python语言进行编码而无法执行此操作。

1 个答案:

答案 0 :(得分:6)

使用soup.create_tag()创建新的<meta>代码,在其上设置属性并将其添加到您的文档<head>

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

演示:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('''\
... <html><head><title>Hello World!</title>
... </head><body>Foo bar</body></html>
... ''')
>>> metatag = soup.new_tag('meta')
>>> metatag.attrs['http-equiv'] = 'Content-Type'
>>> metatag.attrs['content'] = 'text/html'
>>> soup.head.append(metatag)
>>> print soup.prettify()
<html>
 <head>
  <title>
   Hello World!
  </title>
  <meta content="text/html" http-equiv="Content-Type"/>
 </head>
 <body>
  Foo bar
 </body>
</html>
相关问题