Home > Back-end >  Web Scraping: Why is the find command not getting the expected result
Web Scraping: Why is the find command not getting the expected result

Time:12-16

I'm beginning to work on a scraping script for my club. I will hopefully create a script that can collect all the data from by club teams and make my data analysis easier. I'm working on the code below, however I'm struggling to get the text from "actaEquipos". My code should collect the home team and the away team which I will later add into an excel sheet, but I'm stuck on the first step.

from bs4 import BeautifulSoup
from bs4.element import Stylesheet
import requests, openpyxl

excel   =  openpyxl.Workbook()
print(excel.sheetnames)
sheet = excel.active
sheet.title = "Acta Partido"
print (excel.sheetnames)

try:

    source = requests.get('https://www.fcf.cat/acta/2022/futbol-11/cadet-primera-divisio/grup-2/1c/sant-ignasi-ce-a/1c/lhospitalet-centre-esports-b')

    source.raise_for_status()

    soup = BeautifulSoup(source.text,'html.parser')

    actaEquipos = soup.find_all('div', class_='acta-equip')
    actaMarcador = soup.find('div', class_='acta-marcador').text
    acta = soup.find_all('table', class_='acta-table')
    
    print(actaEquipos)

    for equipo in actaEquipos:
        nombreEquipo = equipo.find('span', class_='tr').txt

        print(nombreEquipo)
        print(len(actaEquipos))
        break

except Exception as e:
    print(e)

excel.save('ActaPartido.xlsx')

Can anyone help me understand what I'm missing?

Thanks

CodePudding user response:

What happens?

There is just a typo in your code that should get the text - There is no error shown, cause result of selection is None

How to fix?

Fix typo by adding an e to txt:

nombreEquipo = equipo.find('span', class_='tr').text
  • Related