簡體   English   中英

Python錯誤:TypeError:'NoneType'對象不可調用

[英]Python Error: TypeError: 'NoneType' object is not callable

下面是我的代碼和我得到的錯誤

import requests
import BeautifulSoup
from BeautifulSoup import BeautifulSoup

url = "http://www.indeed.com/jobs?  q=hardware+engineer&l=San+Francisco%2C+CA"

r = requests.get(url)
soup = BeautifulSoup(r.content)

job_titles = soup.find_all("a", {"class", "jobtitle"})

print job_titles

我得到的錯誤:

Traceback (most recent call last):
  File "webscraping.py", line 13, in <module>
    job_titles = soup.find_all("a", {"class", "jobtitle"})
TypeError: 'NoneType' object is not callable

似乎您使用的是BeautifulSoup 3 ,它沒有find_all ,而只有findAll

如果要使用BeautifulSoup 3,請使用findAll

或使用BeautifulSoup 4使用find_all

from bs4 import BeautifulSoup

它表明soup.find_all為None。 確保不是全部。 此外,我在您的代碼中注意到的另一可疑的事情是導入

import BeautifulSoup
from BeautifulSoup import BeautifulSoup

確保您導入其中任何一個並進行相應的修改

soup = BeautifulSoup(r.content)

下面是對我起作用的-工作jobtitleh2類名,而不是a 我與bs4 '4.4.0'在一起

import requests
from bs4 import BeautifulSoup

url = "http://www.indeed.com/jobs?  q=hardware+engineer&l=San+Francisco%2C+CA"

r = requests.get(url)
soup = BeautifulSoup(r.content)

job_titles = soup.find_all("h2", {"class", "jobtitle"})

for job in job_titles:
    print job.text.strip()

Prints-

Management Associate - Access & Channel Management
Airline Customer Service Agent (Korean Speaker preferred) SF...
Event Concierge
Flight Checker
Office Automation Clerk
Administrative Assistant III
Operations Admin I - CA
Cashier Receptionist, Grade 3, (Temporary)
Receptionist/Office Assistant
Full-Time Center Associate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM