2014-02-06 181 views
1

我想用列表的形式獲取數據,而使用pycurl,但它顯示json的對象:( 請問我可以告訴我最簡單的方法如何打破並得到列表嗎? 在此先感謝指導:) 這裏是我的代碼:如何將json轉換爲Python列表?

import pycurl 
import json 
from io import BytesIO 

c = pycurl.Curl() 
data = BytesIO() 

c.setopt(c.URL,'https://api.angel.co/1/jobs') 
c.setopt(c.WRITEFUNCTION, data.write) 
c.perform() 
fetched_data= data.getvalue() 
print (fetched_data); 
+1

爲什麼你使用(繁瑣)'pycurl'庫? Python 3有更好的選擇來下載Web數據。 –

回答

3

解碼與您已經導入json模塊的JSON:

result = json.loads(fetched_data.decode('utf8')) 

我在這裏硬編碼的編碼; JSON RFC狀態默認爲UTF-8,但您可以看到Content-Type響應標頭是否有一個charset參數,該參數可以告訴您實際的編碼。我不想用pycurl;真的是,真的很麻煩來處理。改爲使用requests library,它可以爲您處理JSON,開箱即用。它處理的字節流解碼爲Unicode

import requests 

result = requests.get('https://api.angel.co/1/jobs').json() 

那個特定的URL返回一個JSON 對象,導致Python字典:

{'jobs': [{'angellist_url': 'https://angel.co/jobs?startup_id=267120', 
      'created_at': '2014-02-06T08:07:35Z', 
      'equity_cliff': '0.0', 
      'equity_max': '0.0', 
      'equity_min': '0.0', 
      'equity_vest': '0.0', 
      'id': 22672, 
      'salary_max': 0, 
      'salary_min': 0, 
      'startup': {'angellist_url': 'https://angel.co/nationsroot-1', 
         'community_profile': False, 
         'company_url': 'http://nationsroot.com', 
         'created_at': '2013-09-20T07:55:25Z', 
         'follower_count': 5, 
         'hidden': False, 
         'high_concept': 'Bridge between citizens and politicians', 
         'id': 267120, 
         'logo_url': 'https://s3.amazonaws.com/photos.angel.co/startups/i/267120-9513670dbfe74c170201df0e385d1c2c-medium_jpg.jpg?buster=1379663721', 
         'name': 'NationsRoot', 
         'product_desc': 'NationsRoot helps you find profiles and report cards of politicians, share your thoughts and rate your political leaders. We believe that citizens are not absolved of their duties once they are done voting. So, We provide a platform where you can rate the quality of government provided services to create real time Report Cards for all politicians.\r\n\r\nOn the other hand, Politicians will have the detail analytics about requirements of citizens in various electoral area which will be helpful during elections and can have latest updates too.', 
         'quality': 3, 
         'thumb_url': 'https://s3.amazonaws.com/photos.angel.co/startups/i/267120-9513670dbfe74c170201df0e385d1c2c-thumb_jpg.jpg?buster=1379663721', 
         'updated_at': '2014-02-06T07:36:36Z'}, 
      'tags': [{'angellist_url': 'https://angel.co/business-development-1', 
         'display_name': 'Business Development', 
         'id': 15525, 
         'name': 'business development', 
         'tag_type': 'SkillTag'}, 
         {'angellist_url': 'https://angel.co/sales-strategy-and-management', 
         'display_name': 'Sales Strategy and Management', 
         'id': 16928, 
         'name': 'sales strategy and management', 
         'tag_type': 'SkillTag'}, 
         {'angellist_url': 'https://angel.co/sales-and-marketing-2', 
         'display_name': 'Sales and Marketing', 
         'id': 23989, 
         'name': 'sales and marketing', 
         'tag_type': 'SkillTag'}, 
         {'angellist_url': 'https://angel.co/australia', 
         'display_name': 'Australia', 
         'id': 1618, 
         'name': 'australia', 
         'tag_type': 'LocationTag'}, 
         {'angellist_url': 'https://angel.co/sales-2', 
         'display_name': 'Sales', 
         'id': 80488, 
         'name': 'sales', 
         'tag_type': 'RoleTag'}], 
      'title': 'Sales Intern', 
      'updated_at': '2014-02-06T08:07:57Z'}, 
      # many more entries elided 
      ], 
'last_page': 184, 
'page': 1, 
'per_page': 50, 
'total': 9195} 

你正在尋找那麼列表是result['jobs'],但您還需要額外的頁面才能獲得所有9195個結果。

+0

我做了,但沒有奏效: TypeError:不能在類似字節的對象上使用字符串模式 –

+0

@ user3278658:對,您需要先將結果解碼爲Unicode值。在這種情況下,響應使用UTF-8。 –

+0

謝謝兄弟。 –