【问题标题】:How to convert json to Python List?如何将 json 转换为 Python 列表?
【发布时间】:2014-03-03 02:25:14
【问题描述】:

我想在使用 pycurl 时以列表的形式获取数据,但它显示的是 json 对象:( 你能告诉我最简单的方法如何打破并从中获取列表吗? 在此先感谢您的指导:) 这是我的代码:

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); 

【问题讨论】:

    标签: python json python-3.x pycurl


    【解决方案1】:

    使用您已导入的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 结果。

    【讨论】:

    • 我做了但没用:TypeError: can't use a string pattern on a bytes-like object
    • @user3278658:对,您需要先将结果解码为 Unicode 值。在这种情况下,响应使用 UTF-8。
    • Requests 包是否有助于网页抓取 HTML 页面?如果我想做这样的事情我应该怎么做:requests.get(URL).HTMLparser
    • @Rose:不,requests 仅有助于通过 HTTP 发送和接收数据。使用 BeautifulSoup 之类的东西来解析 HTML。你可以试试RoboBrowser,它将这两个工具合二为一。
    • 谢谢我亲爱的兄弟 :) 让我试试吧!
    猜你喜欢
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 2020-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-19
    相关资源
    最近更新 更多