Home>

I am trying to retrieve the ID of a racehorse from netkeiba's racecard (including the past five race records).
I took it out with BeautifulSoup, but at the end I get an error.

AttributeError: ResultSet object has no attribute'find_all'.You're probably treating a list of  elements like a single element. Did you call find_all() when you meant to call find()?
Applicable source code
import requests
from bs4 import BeautifulSoup
url ='https://race.netkeiba.com/race/shutuba_past.html?race_id=202005021212&rf=shutuba_submenu'
html = requests.get(url)
html.encoding ='EUC-JP'
soup = BeautifulSoup(html.text, "html.parser")
import re
soup.find_all('div', attrs = {'class':'Horse02'}) # No error so far
#The following code gives an error and the character string containing the horse ID (number) cannot be retrieved.
soup.find_all('div', attrs = {'class':'Horse02'}).find_all('a', attrs = {'href': re.compile('^/horse')})
What I tried

Please describe what you have tried for the problem here.

Supplemental information (FW/tool ​​version, etc.)

Jupyter Lab

  • Answer # 1

    .find_all returns a list, so you need to loop around to process each item.
    Also, the regular expression to get the ID part is wrong.
    Regular expressions are an example, but for reference

    for div in soup.find_all('div', attrs = {'class':'Horse02'}):
        print(re.search("horse/(\d+)",str(div.find('a'))).group(1))
    #2015105008
    #2013102360
    #2014106176
    #2015104689
    #2016104376
    #2013105785
    #2012104164
    # 2015101654
    #2014106038
    #2013109072
    #2015104287
    #2015103452
    #2015104671
    #2014102894
    #2016104907
    #2016101209
    #2016104635
    #2016103444

  • Answer # 2

    lst = []
    for item in soup.find_all('div', attrs = {'class':'Horse02'}):
        lst.append(item.find('a', attrs = {'href': re.compile('^/horse')}))
    print(lst)

    Can i go?

Related articles