qwpr
qwpr

Reputation: 49

I am reviving a slightly dated Webscraper, I am getting a invalid syntax error

A few months ago with the help of many beautiful souls on stackoverflow I learned a good amount. However my brain retention is small. I am getting a syntax error trying to run my script. I am sure it is something very basic I am missing, any help would be great. Code and error posted.

import csv
import requests
from bs4 import BeautifulSoup
from time import sleep
from random import randint

# sleep(randint(10,20))


realtor_data = []

for page in range(1, 34):
    print(f"Scraping page {page}...")
    url = f"https://www.realtor.com/realestateagents/hoover_al/pg-{page}"
    soup = BeautifulSoup(requests.get(url).text, "html.parser")

    for agent_card in soup.select("div.agent-list-card-title.mobile-only"):
        name = agent_card.find("div", {"class": "agent-name"})
        number = agent_card.find("div", {"class": "agent-phone"})
        realtor_data.append(
            [
                name.getText().strip(),
                number.getText().strip() if number is not None else "N/A"                
            ],
        )

with open("data.csv", "w") as output:
    w = csv.writer(output)
    w.writerow(["NAME:", "PHONE NUMBER:", "CITY:"])
    w.writerows(realtor_data)

import pandas as pd
a=pd.read_csv("data.csv" , encoding='latin-1')
a2 = a.iloc[:,[0,1]]
a3 = a.iloc[:,[2]]
a3 = a3.fillna("Hoover")
b=pd.concat([a2,a3],axis=1)
b.to_csv("data.csv")

Syntax Error: Python 3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> python hoover.py
  File "<stdin>", line 1
    python hoover.py
           ^
SyntaxError: invalid syntax

Sorry if the formatting is poor.

Upvotes: 0

Views: 74

Answers (1)

SlasherZer0
SlasherZer0

Reputation: 86

See that '>>>' before your command? The problem is that you are trying to call python to execute your file within the python shell.

Try running your command in the terminal, without calling 'python' to execute first.

Upvotes: 1

Related Questions