使用 beautifulsoup 抓取代码的特定部分

Scraping specific part of code with beautifulsoup

我使用 beautifulsoup 库,我想从代码中的特定部分抓取元素,这是我的代码:

#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)

这是我要抓取的部分:

<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>

解决方案是:

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)