What does 'AttributeError: 'NoneType' object has no attribute 'find_all'' mean in this code?

What does 'AttributeError: 'NoneType' object has no attribute 'find_all'' mean in this code?

我正在构建一个非常简单的 beautifulsoup/requests 网络抓取工具,但是当 运行 它出现在求职网站上时,错误

AttributeError: 'NoneType' object has no attribute 'find_all'

出现。 这是我的代码:

import requests
from bs4 import BeautifulSoup

URL = "https://uk.indeed.com/jobs?q&l=Norwich%2C%20Norfolk&vjk=139a4549fe3cc48b"
page = requests.get(URL)

soup = BeautifulSoup(page.content, "html.parser")

results = soup.find(id="ResultsContainer")

job_elements = results.find_all("div", class_="resultContent")

python_jobs = results.find_all("h2", string="Python")

for job_element in job_elements:
    title_element = job_element.find("h2", class_="jobTitle")
    company_element = job_element.find("span", class_="companyName")
    location_element = job_element.find("div", class_="companyLocation")
    print(title_element)
    print(company_element)
    print(location_element)
    print()

有人知道问题出在哪里吗?

检查您的选择器 results 属性 id 应该是 resultsBody。错误的选择器导致使用 results 的行出错,导致 None 没有属性:

results = soup.find(id="resultsBody")

还有 job_elements 它是一个 td 而不是 div:

job_elements = results.find_all("td", class_="resultContent")

您也可以使用 css selectors:

链接选择器
job_elements = soup.select('#resultsBody td.resultContent')

仅获取包含 Python:

的那些
job_elements = soup.select('#resultsBody td.resultContent:has(h2:-soup-contains("Python"))')

例子

import requests
from bs4 import BeautifulSoup

URL = "https://uk.indeed.com/jobs?q&l=Norwich%2C%20Norfolk&vjk=139a4549fe3cc48b"
page = requests.get(URL)

soup = BeautifulSoup(page.content, "html.parser")

results = soup.find(id="resultsBody")

job_elements = results.find_all("td", class_="resultContent")

python_jobs = results.find_all("h2", string="Python")

for job_element in job_elements:
    title_element = job_element.find("h2", class_="jobTitle")
    company_element = job_element.find("span", class_="companyName")
    location_element = job_element.find("div", class_="companyLocation")
    print(title_element)
    print(company_element)
    print(location_element)
    print()