Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Embedding HTML Code in Python
#1
I am trying to embed HTML in Python, not sure how. Tried importing Beautiful Soup but get error.
Reply
#2
BeautifulSoup is for parsing HTML. What code did you use to get the error, and what is the error? What was the installation process?
Recommended Tutorials:
Reply
#3
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']
Reply
#4
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))
Reply
#5
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
Reply
#6
Thanks Snippsat. Works fine!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Populating list items to html code and create individualized html code files ChainyDaisy 0 1,560 Sep-21-2022, 07:18 PM
Last Post: ChainyDaisy
  HTML multi select HTML listbox with Flask/Python rfeyer 0 4,529 Mar-14-2021, 12:23 PM
Last Post: rfeyer
  Python3 + BeautifulSoup4 + lxml (HTML -> CSV) - How to loop to next HTML/new CSV Row BrandonKastning 0 2,328 Mar-22-2020, 06:10 AM
Last Post: BrandonKastning
  How to get the href value of a specific word in the html code julio2000 2 3,143 Mar-05-2020, 07:50 PM
Last Post: julio2000
  spliting html code with br tag yokaso 11 15,687 Aug-07-2019, 03:18 PM
Last Post: snippsat
  Help with Python and HTML code karlo_ds 4 3,390 Oct-16-2017, 03:03 PM
Last Post: karlo_ds

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020