简体   繁体   中英

AttributeError: 'NoneType' object has no attribute 'getText' (Python)

I learning to code a webscraping in python to extract data of a web page.

I found this code in the internet, but it appears this error:

AttributeError: 'NoneType' object has no attribute 'getText'

This is the code:

from bs4 import BeautifulSoup
import requests
import sys

if len(sys.argv)>=2:
    URL=sys.argv[1]

    # url text
    #URL = "https://www.mbet.es/es/live/animation/5610082"
    #

    # Realizamos la petición a la web
    req = requests.get(URL)

    # Comprobamos que la petición nos devuelve un Status Code = 200
    status_code = req.status_code
    if status_code == 200:

        # Pasamos el contenido HTML de la web a un objeto BeautifulSoup()
        html = BeautifulSoup(req.text, "html.parser")

        resultadoytiempo=html.find('td',{'class':'event-description'})

        print ("%s" %resultadoytiempo.getText())


        # Obtenemos todos los divs donde están las entradas sobre precio y resultado 
        entradas = html.find_all('div', {'class': 'nowrap simple-price'})

        # Recorremos todas las entradas para extraer el resultado y su cuota
        for i, entrada in enumerate(entradas):
            # Con el método "getText()" no nos devuelve el HTML
            equipo1 = entrada.find('span', {'class': 'left-simple'}).getText()
            cuota1= entrada.find('span', {'class': 'right-simple'}).getText()

            # Imprimo el equipo/resultado y cuota
            print ("%d - %s  |  %s "  % (i + 1, equipo1, cuota1) )

    else:
        print ("Status Code %d" % status_code)


else:
    print ("Falta argumento: url de la página mbet a analizar");

Thanks :)

I edit the

print ("%s" %resultadoytiempo).getText()

to

print ("%s" %resultadoytiempo.getText())

but nothing changes

This causes getNext to be called on retrun value of print function

print ("%s" %resultadoytiempo).getText()

Use the following

if resultadoytiempo is not None:
    print ("%s" %resultadoytiempo.getText())

print不返回任何内容,因此您不能对它返回的内容调用getText

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM