How can I add background color inside html code using beautifulsoup?
Question: With beautifulsoup I get the html code of a site, let say it's this:

    
    
    <!DOCTYPE html>
    <html>
    <head>
    </head>
    <body>
    <h1>My First Heading</h1>
    <p>My first paragraph.</p>
    </body>
    </html>
    

How I can add this line `body {background-color:#b0c4de;}` inside the **head**
tag using beautifulsoup?

Lets say python code is:

    
    
    #!/usr/bin/python
    
    import cgi, cgitb, urllib2, sys
    from bs4 import BeautifulSoup
    
    site = "www.example.com"
    page = urllib2.urlopen(site)
    soup = BeautifulSoup(page)
    


Answer: You can use:

    
    
    soup.head.append('body {background-color:#b0c4de;}')
    

But you should [create a `<style>`
tag](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#beautifulsoup-new-
string-and-new-tag) before.

For instance:

    
    
    head = soup.head
    head.append(soup.new_tag('style', type='text/css'))
    head.style.append('body {background-color:#b0c4de;}')
    

