#๐Ÿ”’ What Im doing wrong?

46 messages ยท Page 1 of 1 (latest)

pine willow
#

Hello guys, I am trying to make web scrapper with selenium but for some reason I cant run the script

Error:

  File "<stdin>", line 1
    py scraper.py
       ^^^^^^^
SyntaxError: invalid syntax```

Script:
```from selenium.webdriver import Chrome, ChromeOptions
from selenium.webdriver.common.by import By

def get_data(url)->list:
    browser_options = ChromeOptions()
    browser_options_headless = True

    driver = Chrome(options=browser_options)
    driver.get(url)

    element = driver.find_element(By.LINK_TEXT, "Humor")
    element.click()

    books = driver.find_elements(By.CSS_SELECTOR, ".product_pod")
    data = []
    for book in books:
        title = book.find_element(By.CSS_SELECTOR, "h3 > a")
        price = book.find_element(By.CSS_SELECTOR, ".price_color")
        stock = book.find_element(By.CSS_SELECTOR, ".instock,availability")

        book_item = {
            'title':title.get_attribute("title"),
            'price':price.text,
            'stock':stock.text
        }

        data.append(book_item)


    driver.quit()
    return data

def main():
    data = get_data("https://books.toscrape.com/")

    print(data)
    print('script work')

if __name__ == 'main':
    main()```

Im using python 3.12.1, my script is out of .env folder
I tried to run it through powershell, gitbash, cmd and no results, I cant figure out where it stucks but I believe code is good..?
river elkBOT
#

Hey @pine willow!

It looks like you incorrectly specified a language for your code block.

Make sure you put your code on a new line following py. There must not be any spaces after py.

Here is an example of how it should look:
```py
print('Hello, world!')
```

This will result in the following:

print('Hello, world!')```
You can **edit your original message** to correct your code block.
#

@pine willow

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fickle moth
#

how are you trying to run the file?

pine willow
fickle moth
#

from where?

#

where are you running that command?

pine willow
torn burrow
#

You are trying to run your python script from inside a python interactive session.

fickle moth
#

can you show a screenshot of your screen?

torn burrow
#

Run from the terminal prompt (eg gitbash powershell), not from inside a running Python process.

crisp pilot
fickle moth
# pine willow

yes, that is a python shell, you run python code there, not python files

#

try typing exit()

#

and then your command again

pine willow
#

I made a line print('script work') just in case to make sure script works

torn burrow
#

The file you are running probably isn't the file you think it is then. Check it is saved and you don't have an old duplicate in that directory.

supple halo
#

python3 scraper.py

#

?

pine willow
pine willow
crisp pilot
#

and on the directory tree above

#

this part here

#

click on the empty field, type cmd

#

hit enter

#

type python scraper.py

torn burrow
#

He'll run into the issue where Python isn't in his path. Regardless, you can see in vscode he's using a venv with, assumedly, Selenium already installed.

fickle moth
#

pip install run

torn burrow
#

The first execution was successful. The issue is with the script itself.

#

if __name__ == 'main':
This line of your script is wrong and so the rest never runs.

#

if __name__ == "__main__"

#

see the underscores

pine willow
#

Yeah its the issue in script, I added print('test1') on line 4 and I got the output

#

Wow thank you that was the problem, it works now

#

I replaced it with:

    main()```
#

Thank you guys so much

crisp pilot
torn burrow
#

took me a lot of starring to see it

river elkBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.