简体   繁体   中英

how to fix AttributeError

I'm making a web wrapper with python.

but i can't fix the error AttributeError: 'NoneType' object has no attribute 'find'

this is my code

def extract_indeed_jobs(last_page):
    jobs = []
    # for page in range(last_page):
    result = requests.get(f"{URL}&start={0*LIMIT}")
    soup = BeautifulSoup(result.text, "html.parser")
    results = soup.find_all("div", {"class": "jobsearch-SerpJobCard"})
    # print(results)       It works!!

    for result in results:
        # print(results)      It works!!
        title = result.find("h2", {"class:": "title"})
        # i think above line is the problem but i dont konw how to fix it
        # 'results' have <h2> tag, i checked from printed out 'results'
        print(title.find("a"))

    return jobs

i think title = result.find("h2", {"class:": "title"}) is the problem but don't know how to fix it

this code didn't help me

title = result.find("a", {"class":"jobtitle"})["title"]

I appreciate any hint:)

all code:

import requests
from bs4 import BeautifulSoup

LIMIT = 50

URL = f"https://kr.indeed.com/취업?q=python&limit={LIMIT}"


def extract_indeed_pages():
    result = requests.get(URL)

    soup = BeautifulSoup(result.text, "html.parser")

    pagination = soup.find("div", {"class": "pagination"})

    links = pagination.find_all('a')
    pages = []

    for link in links[:-1]:
        pages.append(int(link.string))

    max_page = pages[-1]
    return max_page


def extract_indeed_jobs(last_page):
    jobs = []
    # for page in range(last_page):
    result = requests.get(f"{URL}&start={0*LIMIT}")
    soup = BeautifulSoup(result.text, "html.parser")
    results = soup.find_all("div", {"class": "jobsearch-SerpJobCard"})

    for result in results:
        if not result:
            results.remove(result)

    for result in results:
        title = result.find("h2", {"class:": "title"})
        print(title.find("a"))

    return jobs

What happens?

You try to find() the title that way, but you will never get a match, cause of the attribute class: :

title = result.find("h2", {"class:": "title"})

How to fix that?

Just delete the typo : in your "class:" definition:

title = result.find("h2", {"class": "title"})

Example

import requests
from bs4 import BeautifulSoup

soup = BeautifulSoup(requests.get(f"https://de.indeed.com/jobs?q=Vollzeit&l=Bremen&start=40").text, "html.parser")
results = soup.find_all("div", {"class": "jobsearch-SerpJobCard"})

for result in results:
    title = result.find("h2", {"class": "title"})
    print(title.a.text)

Output

Mediengestalter (Bild&Ton)/ Filmemacher in Teil-/Vollzeit

Freelance (m/w/d)

Immobilienmakler (m/w/x) mit Festgehalt und ungedeckelter Pr...

Fahrer (m/w/d) für Betonmischer in Bremen

Glasreiniger / Gehilfe Vollzeit

Helfer Verpackung (m/w/d)

Ehrenamtliche Helfer (m/w/d)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM