Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9199657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:35:03+00:00 2026-06-17T22:35:03+00:00

html_doc= <html> <head></head> <body> <ul class=navigation></ul> </body> </html> link_doc= <p> <li><a href=http://example.com/elsie id=link1>Elsie</a></li> <li><a

  • 0
html_doc="""
<html>
    <head></head>
    <body>
        <ul class="navigation"></ul>
    </body>
</html>
"""
link_doc="""
<p>
<li><a href="http://example.com/elsie" id="link1">Elsie</a></li>
<li><a href="http://example.com/lacie" id="link2">Lacie</a></li>
<li><a href="http://example.com/tillie" id="link3">Tillie</a></li>
</p>
"""
from bs4 import BeautifulSoup
soup = BeautifulSoup(html_doc)
link = BeautifulSoup(link_doc)

navigation = soup.find_all("ul", {"class" : "navigation"})
links = link.find_all('li')

for i in range(0,3): # assume I would like to write to 3 files
    for n in range(len(links)):
        navigation[0].append(links[n])
    output = navigation[0].prettify(formatter=None)
    file = open(str(i) + '.html', 'w')
    file.write(output)
    file.close()
    navigation[0].clear()    

I have two simple documents like this. What I’d like to do is add some links between <ul class="navigation"> and write them to 3 files.

But also I’d like to add class attributes active to the <li> element depending on file order. For example 0.html should be like this:

<ul class="navigation">
 <li class="active">
  <a href="http://example.com/elsie" id="link1">
   Elsie
  </a>
 </li>
 <li>
  <a href="http://example.com/lacie" id="link2">
   Lacie
  </a>
 </li>
 <li>
  <a href="http://example.com/tillie" id="link3">
   Tillie
  </a>
 </li>
</ul>

1.html would be like this etc.

<ul class="navigation">
 <li>
  <a href="http://example.com/elsie" id="link1">
   Elsie
  </a>
 </li>
 <li class="active">
  <a href="http://example.com/lacie" id="link2">
   Lacie
  </a>
 </li>
 <li>
  <a href="http://example.com/tillie" id="link3">
   Tillie
  </a>
 </li>
</ul>

How can I do that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:35:04+00:00Added an answer on June 17, 2026 at 10:35 pm

    EDIT: I added a much better way of inserting a link. I hope this helps!

    edit x2: This should work now:

    Adding links in:

    from bs4 import BeautifulSoup
    html = '<ul class="navigation"> foo <i>hi</i> bar</ul>'
    soup = BeautifulSoup(html)
    original_tag = soup.ul
    new_tag = soup.new_tag('a', href='http://www.example.com')
    new_tag.insert(0, 'Text!')
    original_tag.append(new_tag)
    print original_tag
    # Prints:
    # <ul class="navigation"> foo <i>hi</i> bar<a href="http://www.example.com">Text!</a></ul>
    # If you don't want it to go to the last bit of the tag all the time, 
    # use original_tag.insert(). Here's a link to the documentation about it:
    # http://www.crummy.com/software/BeautifulSoup/bs4/doc/#insert
    

    For adding your class=”active”, replace your last for loop with this:

    for i in range(0,3): # assume I would like to write to 3 files
        for n in range(len(links)):
            navigation[0].append(links[n])
        output = navigation[0].prettify(formatter=None)
        soup2 = BeautifulSoup(output)
        mylist = soup2.find_all('li')
        mylist[i]['class'] = 'active'
        filee = open(str(i) + '.html', 'w') # I changed it to filee to avoid confusion with the file function
        filee.write(str(soup2))
        filee.close()
        navigation[0].clear()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following html snippet <!DOCTYPE html> <html> <head> <script type=text/javascript src=http://code.jquery.com/jquery-1.4.2.js ></script> <script
I have this html code: <html> <head> ... </head> <body> <div> <div class=foo data-type=bar>
I have an HTML doc that looks roughly like this: <html> <head><title>Example</title></head> <body> <div
I'am trying to understand the example from program_options of the boost library ( http://www.boost.org/doc/libs/1_38_0/doc/html/program_options/tutorial.html#id3761458
I'm confused by this: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/KeyValueCoding/Concepts/AccessorConventions.html#//apple_ref/doc/uid/20002174-178830-BAJEDEFB Supposing @interface Office : NSObject { NSMutableArray *employees; }
I got the following very basic template: <html> <head> </head> <body> <div> <!-- Using
I have some test html page <!DOCTYPE html> <html lang=en xmlns=http://www.w3.org/1999/xhtml> <head> <meta charset=utf-8
I am trying to populate a chart with button click. <html> <head> <script src=https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js></script>
I have a page with next code: <HTML> <HEAD> <TITLE>smth</TITLE> <META HTTP-EQUIV=Content-Type CONTENT=text/html; charset=UTF-8>
this is inside my sample.html <html> <head> <title>Test</title> </head> <body> <div id=testingID>hello</div> </body> </html>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.