Python错误:TypeError:'NoneType'对象不可调用

时间:2015-11-19 07:03:17

标签: python web-scraping beautifulsoup typeerror

以下是我的代码和我得到的错误

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

3 个答案:

答案 0 :(得分:2)

您好像使用的BeautifulSoup 3没有find_all,只有findAll

如果您使用BeautifulSoup 3,请使用findAll

或使用BeautifulSoup 4来使用find_all

from bs4 import BeautifulSoup

答案 1 :(得分:1)

它显示soup.find_all为None。确保它不是没有。此外,我在您的代码中注意到的另一个可疑的事情是导入

import BeautifulSoup
from BeautifulSoup import BeautifulSoup

确保您导入其中任何一个并相应地修改

soup = BeautifulSoup(r.content)

答案 2 :(得分:0)

以下是我的工作 - 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()

打印 -

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