2015-10-16 194 views
-2

我调试了这段代码,但仍然有一些错误,我不知道如何处理这个问题。AttributeError:'NoneType'对象没有属性'get'?

我已经搜索了类似的问题,但仍然有一些问题。

import requests 
from bs4 import BeautifulSoup 
import time 
import json 
import os 
import sys 
reload(sys) 
sys.setdefaultencoding('utf-8') 


url = 'http://www.zhihu.com' 
loginURL = 'http://www.zhihu.com/login/email' 

headers = { 
"User-Agent": 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:41.0) Gecko/20100101 Firefox/41.0', 
"Referer": "http://www.zhihu.com/", 
'Host': 'www.zhihu.com', 
} 

data = { 
'email': '[email protected]', 
'password': 'xxxxxxx', 
'rememberme': "true", 
} 

s = requests.session() 

if os.path.exists('cookiefile'): 
with open('cookiefile') as f: 
    cookie = json.load(f) 
s.cookies.update(cookie) 
req1 = s.get(url, headers=headers) 
with open('zhihu.html', 'w') as f: 
    f.write(req1.content) 

else: 
req = s.get(url, headers=headers) 
print req 

soup = BeautifulSoup(req.text, "html.parser") 
xsrf = soup.find('input', {'name': '_xsrf', 'type': 'hidden'}).get('value') 

data['_xsrf'] = xsrf 

timestamp = int(time.time() * 1000) 
captchaURL = 'http://www.zhihu.com/captcha.gif?=' + str(timestamp) 
print captchaURL 

with open('zhihucaptcha.gif', 'wb') as f: 
    captchaREQ = s.get(captchaURL) 
    f.write(captchaREQ.content) 
loginCaptcha = raw_input('input captcha:\n').strip() 
data['captcha'] = loginCaptcha 
print data 
loginREQ = s.post(loginURL, headers=headers, data=data) 
print loginREQ.url 
print s.cookies.get_dict() 
with open('cookiefile', 'wb') as f: 
    json.dump(s.cookies.get_dict(), f) 

# http://www.zhihu.com/question/27621722/answer/48820436. 
zanBaseURL = 'http://www.zhihu.com/answer/14926794/voters_profile?&offset={0}' 
page = 0 
count = 0 
while 1: 
zanURL = zanBaseURL.format(str(page)) 
page += 10 
zanREQ = s.get(zanURL, headers=headers) 
zanData = zanREQ.json()['payload'] 
if not zanData: 
    break 
for item in zanData: 
    zansoup = BeautifulSoup(item, "html.parser").find(
     'a', {'target': "_blank", 'class': 'zg-link'}) 
    print 'nickname:', zansoup.get('title'), ' ', 
    print 'person_url:', zansoup.get('href') 
    count += 1 
print count 

和错误如下:

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 682, in runfile 
    execfile(filename, namespace) 
    File "D:\anzhuang\Anaconda\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 71, in execfile 
    exec(compile(scripttext, filename, 'exec'), glob, loc) 
    File "D:/python/crawl_zhihu.py", line 78, in <module> 
    print 'nickname:', zansoup.get('title'), ' ', 
AttributeError: 'NoneType' object has no attribute 'get' 
+2

那么,错误是很明确的:一'None'类型没有任何属性。所以,看起来你的对象'zansoup'是错误的来源。确认它已被正确分配? –

回答

0

从DOX:

If find() can’t find anything, it returns None

这果然引发错误,因为None类型没有任何属性。

所以,你可以处理可能的None类型:

if zansoup: 
    print 'nickname:', zansoup.get('title'), ' ', 
    print 'person_url:', zansoup.get('href') 
else 
    print 'there was an error ...'