Migrate javascript scraper to python

33 Views Asked by At

I have the following code in Javascript:

const puppeteer = require('puppeteer');

async function run () {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.goto('https://www.soccerstand.com/soccer/argentina/liga-profesional/results/');

    const matches = await page.$$('.event__match');

    const limit = 10;

    for (let i = 0; i < limit; i++) {
        const match = matches[i];
        const evenTime = await match.$eval('.event__time', el => el.innerText);
        const home = await match.$eval('.event__participant--home', el => el.innerText);
        const away = await match.$eval('.event__participant--away', el => el.innerText);
        const scoreHome = await match.$eval('.event__score--home', el => el.innerText);
        const scoreAway = await match.$eval('.event__score--away', el => el.innerText);
        const partHome = await match.$eval('.event__part--home', 
            el => el.innerText.replace('(','').replace(')', ''));
        const partAway = await match.$eval('.event__part--away', 
            el => el.innerText.replace('(','').replace(')', ''));

        console.log(`${evenTime} ${home} - ${away} ${scoreHome}-${scoreAway(${partHome}-${partAway})`);
    }

    await browser.close();
}

run();

It works correctly. I rewrote this code in Python, but it doesn't work. Here is the code in Python:

import asyncio
from pyppeteer import launch

async def run():

    browser = await launch()

    page = await browser.newPage()

    await page.goto('https://www.soccerstand.com/soccer/argentina/liga-profesional/results/')

    matches = await page.querySelectorAll('.event__match')

    limit = 10

    for i in range(limit):
        match = matches[i]
        evenTime = await match.querySelectorEval('.event__time', 'el => el.innerText')
        home = await match.querySelectorEval('.event__participant--home', 'el => el.innerText')
        away = await match.querySelectorEval('.event__participant--away', 'el => el.innerText')
        scoreHome = await match.querySelectorEval('.event__score--home', 'el => el.innerText')
        scoreAway = await match.querySelectorEval('.event__score--away', 'el => el.innerText')
        partHome = await match.querySelectorEval('.event__part--home', 'el => 
            el.innerText.replace("(", "").replace(")", "")')
        partAway = await match.querySelectorEval('.event__part--away', 'el => 
            el.innerText.replace("(", "").replace(")", "")')

        print(f'{evenTime} {home} - {away} {scoreHome} - {scoreAway} {partHome} - {partAway}')

    await browser.close()

asyncio.get_event_loop().run_until_complete(run())

I got the following error:

$ python soccerstand.py
Traceback (most recent call last):
  File "C:\Disk D\Python\Pyppeteer\soccerstand.py", line 38, in <module>
    asyncio.get_event_loop().run_until_complete(run())
  File "C:\Program Files\Lib\asyncio\base_events.py", line 653, in run_until_complete
    return future.result()
       ^^^^^^^^^^^^^^^
  File "C:\Disk D\Python\Pyppeteer\soccerstand.py", line 22, in run
    match = matches[i]
        ~~~~~~~^^^
IndexError: list index out of range

I checked what is stored in the matches variable, but there is an empty list.

I also checked what is stored in the matches variable in Javascript and there an array with elements is displayed.

What is wrong with the Pyton code?

0

There are 0 best solutions below