python - Add meta tag using BeautifulSoup -
how add meta tag after title tag in html page using beautiful soup(library). using python language coding , unable this.
use soup.create_tag() create new <meta> tag, set attributes on , add document <head>.
metatag = soup.new_tag('meta') metatag.attrs['http-equiv'] = 'content-type' metatag.attrs['content'] = 'text/html' soup.head.append(metatag) demo:
>>> 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>
Comments
Post a Comment