Becky
Becky

Reputation: 3

unexpected indent python 3.7 pycharm

I am very new to python. But I want to extract some data of job postings from an online job portal.

With the following code I wanted to extract the title of the job posting of a particular website:

def jobtitle(soup):
    jobs=[]
        for div in soup.find_all(name="div", attrs={"class"}:"row"}):
            for a in div.find_all(name="a",attrs={"data-tn-element":"jobTitle"}):
            jobs.append(a["title"])
    return(jobs)
jobtitle(soup) 

I receive this error message:

for div in soup.find_all(name="div", attrs={"class"}:"row"}):
^
IndentationError: unexpected indent

I tried many different things that were recommend on other sites, but nothing worked. I just don't know what the problem is. I tried different whitespace, but I just don't understand what I am doing wrong.

Any ideas? I would be really grateful!

Thanks a lot :-)

Upvotes: 0

Views: 2528

Answers (1)

Paul Zaino
Paul Zaino

Reputation: 100

Remove the indent on the first for line. The first for statement should be directly under the jobs=[] declaration.

def jobtitle(soup):
    jobs=[]
    for div in soup.find_all(name="div", attrs={"class"}:"row"}):
        for a in div.find_all(name="a",attrs={"data-tn-element":"jobTitle"}):
            jobs.append(a["title"])
    return(jobs)
jobtitle(soup)

Upvotes: 4

Related Questions