2017-09-02 147 views
0

我以前曾被問過這個問題,但還是沒有得到解決方案,我在網上搜索就像沒有人遇到過與我一樣的問題。是否有關於xlwt的錯誤,我已安裝?或者json有什麼問題? 有人能幫助我嗎?我是python的新手。 非常感謝! 的KeyError異常:關於python keyerror的一個錯誤

# -*- coding:utf-8 -*- 
import requests 
import time 
import random 
import xlwt 
import json 

def post_request(url=None, para={}, headers={}): 
    print 'Downloading: ' + str(para['pn']) 
    req = requests.post(url, data=para, headers=headers) 
    return req 

if __name__ == '__main__': 
    url = 'https://www.lagou.com/jobs/positionAjax.json?needAddtionalResult=false&isSchoolJob=0' 
    headers = { 
     'Host':'www.lagou.com', 
     'Referer':'https://www.lagou.com/jobs/list_%E6%95%B0%E6%8D%AE%E5%88%86%E6%9E%90?labelWords=&fromSearch=true&suginput', 
     'User-Agent':'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.96 Mobile Safari/537.36' 
    } 

    pn = 1 
    end = 31 
    for x in range(pn, end, 1): 
     para = { 
      'first': 'true', 
      'kd': 'Python', 
      'pn': pn 
     } 
     req = post_request(url, para, headers) 
     path = 'd://PyCharmProject//lagou_scraping//Python_search_result//' 
     f = open(path + para['kd'] + '-' + str(para['pn']) + '.json', 'wb') 
     f.write(req.content) 
     f.close() 
     time.sleep(random.randint(3, 8)) 
     pn = pn + 1 
def read_json(path): 
    f = open(path) 
    dictory = json.load(f, encoding='utf-8') 
    return dictory 

if __name__ == '__main__': 
    xls = xlwt.Workbook() 
    sheet = xls.add_sheet('native') 
    sheet.write(0, 0, 'positionName') 
    sheet.write(0, 1, 'salary') 
    sheet.write(0, 2, 'education') 
    sheet.write(0, 3, 'workYear') 
    sheet.write(0, 4, 'city') 
    sheet.write(0, 5, 'companyShortName') 
    sheet.write(0, 6, 'companySize') 
    sheet.write(0, 7, 'financeStage') 
    sheet.write(0, 8, 'industryField') 
    sheet.write(0, 9, 'jobNature') 
    sheet.write(0, 10, 'companyLogo') 

    sheetPosition = {'row':1, 'col':0} 
    pn = 1 
    for x in range(pn, 31, 1): 
     path = 'd://PyCharmProject//lagou_scraping//Python_search_result//Python-' + str(pn) + '.json' 
     dictory = read_json(path) 
     row = sheetPosition['row'] 
     col = sheetPosition['col'] 
     pn = pn + 1 
     for x in dictory['content']['positionResult']['result']: 
      sheet.write(row, col, x['positionName']) # 0 
      col = col + 1 
      sheet.write(row, col, x['salary']) # 1 
      col = col + 1 
      sheet.write(row, col, x['education']) # 2 
      col = col + 1 
      sheet.write(row, col, x['workYear']) # 3 
      col = col + 1 
      sheet.write(row, col, x['city']) # 4 
      col = col + 1 
      sheet.write(row, col, x['companyShortName']) # 5 
      col = col + 1 
      sheet.write(row, col, x['companySize']) # 6 
      col = col + 1 
      sheet.write(row, col, x['financeStage']) # 7 
      col = col + 1 
      sheet.write(row, col, x['industryField']) # 8 
      col = col + 1 
      sheet.write(row, col, x['jobNature']) # 9 
      col = col + 1 
      sheet.write(row, col, x['companyLogo']) # 10 
      col = col + 1 

      col = 0 
      row = row + 1 
      sheetPosition = {'row': row, 'col': col} 
    xls.save('d://PyCharmProject//lagou_scraping//lagou_python.xls') 
+0

如果'dictory'沒有一個名爲'content''的鍵,那麼這行代碼:'for x in dictory ['content'] ['positionResult'] ['result']'會產生一個'KeyError'。你爲什麼不打印「勝利」的鑰匙,看看裏面有什麼?你也有兩個嵌套for循環變量'x',這幾乎肯定是一個錯誤。 'pn = pn + 1'(在倒數第二個for循環中)行不做任何事情,因爲'pn'永遠不會再使用。 –

+0

是的,我發現我的錯誤,謝謝! – dik11

回答

0

好像你的錯誤「內容」是從這裏來的:

f = open(path + para['kd'] + '-' + str(para['pn']) + '.json', 'wb') 
    f.write(req.content) # <-- Here is the probable error. 
    f.close() 

也許你必須先檢查狀態碼或嘗試塊 捕獲錯誤:

#using status code 200 
#some server may return different status code 
#other than 200, check how the server return on success 
if(req.status_code == 200): 
    #now you're ok to resume with your code above 
else: 
    #since you're in the for loop, skip this iteration 
    pass 

我希望這有助於。