Python Forum
Embedding HTML Code in Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: Embedding HTML Code in Python (/thread-15670.html)



Embedding HTML Code in Python - kendias - Jan-27-2019

I am trying to embed HTML in Python, not sure how. Tried importing Beautiful Soup but get error.


RE: Embedding HTML Code in Python - metulburr - Jan-27-2019

BeautifulSoup is for parsing HTML. What code did you use to get the error, and what is the error? What was the installation process?


RE: Embedding HTML Code in Python - snippsat - Jan-27-2019

Explain better what you mean bye embedding HTML Code in Python?
(Jan-27-2019, 12:37 AM)kendias Wrote: Tried importing Beautiful Soup but get error.
What error?
BeautifulSoup is for web-scraping and not for embedding HTML Code.
Can in way embed HTML code with triple quote,when learning BS.
from bs4 import BeautifulSoup
 
# Simulate a web page
html = '''\
<body>
  <div id='images'>
    <a href='image1.html'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
    <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
    <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
  </div>
</body>'''

soup = BeautifulSoup(html, 'html.parser')
print([link.get('href') for link in soup.find_all('a')])
Output:
['image1.html', 'image2.html', 'image3.html']



RE: Embedding HTML Code in Python - kendias - Jan-27-2019

Here is my code. I am using Enthought Canopy. I get error invalid syntax on line 4 i.e.
<lat>

from bs4 import BeautifulSoup
soup = BeautifulSoup(xml, 'html.parser')
lat = soup.find('latitude')
<lat>
 <latitude>1.123123</latitude>
</lat>
print((lat.text))



RE: Embedding HTML Code in Python - snippsat - Jan-27-2019

So my guess was correct,if look at my example code do you not see your error?
Can not just past in HTML code,have to put it in triple quote.
from bs4 import BeautifulSoup

xml = '''\
<lat>
  <latitude>1.123123</latitude>
</lat>'''

soup = BeautifulSoup(xml, 'html.parser')
lat = soup.find('latitude')
print((lat.text)) 
Output:
1.123123



RE: Embedding HTML Code in Python - kendias - Jan-27-2019

Thanks Snippsat. Works fine!