Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BeautifulSoup create a <img /> tag

I need to create a <img /> tag. BeautifulSoup creates an image tag like this with code I did:

soup = BeautifulSoup(text, "html5")
tag = Tag(soup, name='img')
tag.attrs = {'src': '/some/url/here'}
text = soup.renderContents()
print text

Output: <img src="/some/url/here"></img>

How to make it? : <img src="/some/url/here" />

It can be of course done with REGEX or similar chemistry. However I was wondering maybe there is any standard way to produce tags like this?

like image 400
garmoncheg Avatar asked Jan 19 '15 17:01

garmoncheg


People also ask

How do you add a tag on BeautifulSoup?

A new tag can be created by calling BeautifulSoup's inbuilt function new_tag(). Inserting a new tag using the append() method : The new tag is appended to the end of the parent tag.

What is a tag in BeautifulSoup?

A HTML tag is used to define various types of content. A tag object in BeautifulSoup corresponds to an HTML or XML tag in the actual page or document. >>> from bs4 import BeautifulSoup >>> soup = BeautifulSoup('<b class="boldest">TutorialsPoint</b>') >>> tag = soup. html >>> type(tag) <class 'bs4.element.Tag'>

What is prettify in BeautifulSoup?

Beautiful Soup is a Python package for parsing HTML and XML documents. It creates a parse tree for parsed pages that can be used to extract data from HTML, which is useful for web scraping. Prettify() function in BeautifulSoup will enable us to view how the tags are nested in the document.


1 Answers

Don't use Tag() to create new elements. Use the soup.new_tag() method:

soup = BeautifulSoup(text, "html5")
new_tag = soup.new_tag('img', src='/some/url/here')
some_element.append(new_tag)

The soup.new_tag() method will pass along the correct builder to the Tag() object, and it is the builder that is responsible for recognising <img/> as an empty tag.

Demo:

>>> from bs4 import BeautifulSoup
>>> soup = BeautifulSoup('<div></div>', "html5")
>>> new_tag = soup.new_tag('img', src='/some/url/here')
>>> new_tag
<img src="/some/url/here"/>
>>> soup.div.append(new_tag)
>>> print soup.prettify()
<html>
 <head>
 </head>
 <body>
  <div>
   <img src="/some/url/here"/>
  </div>
 </body>
</html>
like image 68
Martijn Pieters Avatar answered Sep 21 '22 23:09

Martijn Pieters