Home > Software design >  Scraping specific part of code with beautifulsoup
Scraping specific part of code with beautifulsoup

Time:12-07

I use the beautifulsoup library and I would like to scrape elements from a particular part in the code, here is my code:

#Partie scrapping
soup = BeautifulSoup(page.content, 'html.parser')
print(soup.title) #Permet d'afficher toute la page
print("Récupération de vos notes en cours...")

#Récupération des titres
liste_titres = soup.find_all("span", class_="ui-column-title")
div_notes = soup.find_all("tr", role="row")
for i in liste_titres:
    if i == None:
        print("La case est vide")
    else:
        print(i.string, end=" ")

#Recuperation des matieres
notes = soup.find_all("tr", class_="ui-widget-content ui-datatable-even odd-row")
for i in notes:
    
    if i == None:
        print("La case est vide")
    else:
        print(i)

Here is the part I would scrape:

<tr data-ri="0" class="ui-widget-content ui-datatable-even odd-row" role="row"><td role="gridcell" style="width:250px" class="mg_inherit_bg"><span class="mg_inherit_color" style="font-weight: bold; font-size: 11px">Algorithmique et structure de données</span></td><td role="gridcell" style="width:100px;"><span style="font-weight: bold; font-size: 11px; color: rgb(93, 93, 93); --darkreader-inline-color:#ada69c;" data-darkreader-inline-color="">M. GABER</span></td><td role="gridcell" style="width:37px;text-align: center">2.00</td><td role="gridcell" style="width:37px;text-align: center">2.00</td><td role="gridcell" style="width:45px;text-align: center">16,5</td><td role="gridcell" style="width:45px;text-align: center"></td><td role="gridcell" style="width:45px;text-align: center"></td><td role="gridcell" style="width:55px;text-align: center"></td></tr>

CodePudding user response:

The solution was just:

notes = soup.find_all("tr", class_="ui-widget-content ui-datatable-even odd-row")
for i in notes:
  if i == None:
      print("La case est vide")
  else:
      print(i.td.string)
  • Related