I am using BeautifulSoup to create and write html file . I am able to create a simple html as shown below for MWE. However, all find functions return nothing, so unable to perform further operations (insert, append).
- What is happening?
- How do I set a style to one of the divs alone? (for eg, div2 and div3 shoudl have display:none which later I plan to enable via script)
MWE:
head_soup = BeautifulSoup(open(nbheader_template),"html.parser")
head_soup.contents[0]
base_template = "<!DOCTYPE html><html></html>"
main_soup = BeautifulSoup(base_template,"html.parser")
main_soup.html.append(head_soup) # add nbconver header
# INSERT THE BODY AS IT IS
# bodies = [body.replace('<body>','').replace('</body>','') for body in bodies] # no need of body tags
bodies = ['<div>Test div' + str(i+1) + '</div>' for i in range(3)] # for MWE
body_tag = main_soup.new_tag('body')
for each_body in bodies:
body_tag.append(BeautifulSoup(each_body,'html.parser'))
main_soup.html.insert(1,body_tag)
with open(output_filename, "w") as file:
file.write(str(main_soup))
print(main_soup.find_all('head'))
print(main_soup.html.find_all('head'))
print(main_soup.find_all('body'))
print(main_soup.html.find_all('body'))
print(main_soup.find_all('div'))
print(main_soup.html.find_all('div'))
Context: I am trying to combine multiple jupyter notebook html files. After this update, I need to add styles to individual divs corresponding to each html (each notebook) file.
Here is the nbviewer head