簡體   English   中英

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

數據存儲在 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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM