Python Forum

Full Version: Embedding HTML Code in Python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to embed HTML in Python, not sure how. Tried importing Beautiful Soup but get error.
BeautifulSoup is for parsing HTML. What code did you use to get the error, and what is the error? What was the installation process?
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']
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))
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
Thanks Snippsat. Works fine!