2017-09-17 2 views
0

내 연구를 위해 페이 스북 페이지를 긁어 야하고 이것에 문제가 있습니다. 나는 파이썬으로 초보자입니다. 여기어떻게 파이썬 2.7 키 오류를 해결할 수 있습니까? (srcape 페이 스북 페이지에 시도)

https://github.com/precognox-admin/FBscraper/blob/master/src/scrapeFB.py 코드의 일부입니다 : 내가 GitHub의에 파이썬 코드를 발견

Traceback (most recent call last): 
    File "FBscraper/src/scrapeFB.py", line 325, in <module> 
    s.scrape() 
    File "FBscraper/src/scrapeFB.py", line 264, in scrape 
    self.write_data(d) 
    File "FBscraper/src/scrapeFB.py", line 91, in write_data 
    person_name = message['from']['name'] 
KeyError: 'from' 

:

def write_data(self, d): 

    date_inserted = datetime.now().strftime("%Y-%m-%d %H:%M:%S") 
    messages = d['data'] 
    self.no_messages += len(messages) 

    for message in messages: 

     person_name = message['from']['name'] 
     person_id = message['from']['id'] 
     person_hash_id = hashlib.md5(person_id.encode('utf-8')).hexdigest() 
     published_date = message['created_time'] 
     published_date = datetime.strptime(published_date, "%Y-%m-%dT%H:%M:%S+%f").strftime("%Y-%m-%d %H:%M:%S") 
     post_type = message['type'] 
     post_id = message['id'] 
     org_id = post_id.split('_')[0] 
     status_id = post_id.split('_')[1] 
     post_link = 'https://www.facebook.com/%s/posts/%s' % (org_id, status_id) 

내가이 오류 메시지가 얻을 평 파일을 실행하려면

나는 'from'이라는 키가 존재하지 않지만 반드시 있어야한다는 것을 의미한다는 것을 알고 있습니다. 내가 문서화 및 whatched를 읽고 YouTube와 다른면에 tutotrials을 읽은

{ 
    "posts": { 
    "data": [ 
     { 
     "comments": { 
      "data": [ 
      { 
       "created_time": "2017-09-17T08:42:41+0000", 
       "from": { 
       "name": "Rafiq Ahmed", 
       "id": "10207278423025677" 
       }, 
       "message": "Admirable and applaudable under the 
circumstances!", 
       "id": "10155950241431323_10155950367031323" 
      } 

: 다음은 예를 들어 JSON이다. 나는 .get() 메소드도 시도했지만 작동하지 않는다. :/

답변

0

시도해야합니다. messages = d['data'][0]['data'][0]

관련 문제