'How do I scrape data from this html tags using python and bs4

I am able to scrape data successfully, how I want to write this to a csv file (comma separated)

I want to extract the elements with class "ui-h2", "help__content help__content--small" and "exam-name". I have the code, but as an output I want

  • April 2022, 3 Apr 2022 , UPPSC ACF RFO Mains
  • April 2022, 3 Apr 2022 , MPSC Group C
<div class="item-name" data-toggle="collapse" data-target="#exam-4" aria-expanded=false>
  <div class="ui-h2">April 2022 <span class="ui-tag grey-transparent">14 Exams</span></div>
</div>
<div class="item-details collapse " id="exam-4" data-parent="#exam-month">
  <div class="row">
      <div class="col-12 col-lg-4">
          <div class="ui-card hover-scale">
              <a href="https://testbook.com/uppsc-acf-rfo" class="card-link exam-cards">
                  <div>
                      <span class="icon calendar-icon"></span>
                      <span class="help__content help__content--small">3 Apr 2022</span>
                      <span class="ui-tag green-filled">Official</span>
                  </div>
                  <div class="footer-container">
                      <span class="exam-icon">
                      <img src="https://blogmedia.testbook.com/blog/wp-content/uploads/2020/06/uttar-pradesh-logo-png-8-5bbbec3b.png" height="30">
                      </span>
                      <span class="exam-name" title="UPPSC ACF RFO Mains">UPPSC ACF RFO Mains</span>
                      <span class="exam-cta">
                      Know More <span class="right-icon"></span>
                      </span>
                  </div>
              </a>
          </div>
      </div>
      <div class="col-12 col-lg-4">
          <div class="ui-card hover-scale">
              <a href="https://testbook.com/mpsc-group-c" class="card-link exam-cards">
                  <div>
                      <span class="icon calendar-icon"></span>
                      <span class="help__content help__content--small">3 Apr 2022</span>
                      <span class="ui-tag green-filled">Official</span>
                  </div>
                  <div class="footer-container">
                      <span class="exam-icon">
                      <img src="https://blogmedia.testbook.com/blog/wp-content/uploads/2020/03/mpsc-logo-1-44a80da2.png" height="30">
                      </span>
                      <span class="exam-name" title="MPSC Group C">MPSC Group C</span>
                      <span class="exam-cta">
                      Know More <span class="right-icon"></span>
                      </span>
                  </div>
              </a>
          </div>
      </div>
</div>
</div>
for contents in soup.find_all("div", {"class":"ui-h2"}):
    #print(contents)
    if contents.text is not None:
            #print(contents.text)
            f.write(contents.text+"-")

    for contentspan2 in soup.find_all("span", {"class":"help__content help__content--small"}):
        
        if contentspan2.string is not None:
            #print(contentspan2.string)
            f.write(contentspan2.string+",")
            

        for contentspan in soup.find_all("span", {"class":"exam-name"}):
        
            if contentspan.string is not None:
                #print(contentspan.string)
                f.write(contentspan.string+"\n")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source