【问题标题】:Scraping specific part of code with beautifulsoup用 beautifulsoup 抓取代码的特定部分
【发布时间】:2022-01-10 11:37:11
【问题描述】:

我使用 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_="odd-row")

标签: python web web-scraping beautifulsoup


【解决方案1】:

解决办法是:

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)

【讨论】:

    猜你喜欢
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多