繁体   English   中英

如何修复属性错误

[英]how to fix AttributeError

我正在使用 python 制作 web 包装器。

但我无法修复错误AttributeError: 'NoneType' object has no attribute 'find'

这是我的代码

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

我认为title = result.find("h2", {"class:": "title"})是问题,但不知道如何解决

这段代码对我没有帮助

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

我很感激任何提示:)

所有代码:

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

怎么了?

您尝试以这种方式find()标题,但您永远不会得到匹配,原因是属性class:

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

如何解决?

只需删除错字:在您的“类:”定义中:

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

例子

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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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