利用 requests 库请求 Instagram 的 API 返回的是字符串,怎样转换成字典? - V2EX
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
MagaFun

利用 requests 库请求 Instagram 的 API 返回的是字符串,怎样转换成字典?

  •  
  •   MagaFun Sep 21, 2017 2901 views
    This topic created in 3155 days ago, the information mentioned may be changed or developed.

    最近在做一些 Instagram 相关的项目,Python 上并没有可靠的 Instagram API,所以决定直接使用 requests 库。以下为代码段:

    def getMe(): while True: mediaMe = requests.get('https://api.instagram.com/v1/users/self/media/recent/?access_token={0}&count=1'.format(access_token)) print mediaMe.content print type(mediaMe.content) yield ast.literal_eval(mediaMe.content) with open('instagram.log', 'a') as log: getMe() for line in getMe(): log.write(json.dumps(line)) log.write('\n') print type(line) 

    然后问题出现了,mediaMe 返回的是一个类,mediaMe.content 返回的类型是一个字符串,这个字符串是被字符化的一个词典。尝试使用 ast.literal_eval(mediaMe.content) 转换为词典的时候,却提示 Malformed string。请问有什么解决的方法吗?先提前谢谢大家。

    7 replies    2017-09-22 20:10:52 +08:00
    Cooky
        1
    Cooky  
       Sep 21, 2017 via Android
    json.loads
    janxin
        2
    janxin  
       Sep 21, 2017 via iPad   1
    mediaMe.json()
    iyaozhen
        3
    iyaozhen  
       Sep 21, 2017 via Android
    你这代码写的有点看不懂呀。
    getMe 是个死循环?
    然后 getMe 是个迭代生成器,但遍历这个迭代器的之前又调用了下?
    mec
        4
    mec  
       Sep 21, 2017
    response 对象不是有个 json 方法吗?
    timonwong
        5
    timonwong  
       Sep 21, 2017
    1. response 有个 json() 方法
    2. response 的 json() 方法调用 text 属性
    3. text 属性,如果 response 没有 Content-Encoding 会执行一次巨慢的 chardet
    4. 因此在调用 text/json() 之前,请 response.encoding = 'utf-8' ( instagram 的 API 不返回 Content-Encoding 头)
    timonwong
        6
    timonwong  
       Sep 21, 2017
    我纠正一下上面的第 4 点,Content-Type 里面有 charset 了,不用强设
    MagaFun
        7
    MagaFun  
    OP
       Sep 22, 2017
    @janxin 问题是 mediaMe 返回的是一个 Response 200,必须要用 mediaMe.content 才能解出数据来
    About     Help     Advertise     Blog     API     FAQ     Solana     5813 Online   Highest 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 61ms UTC 06:43 PVG 14:43 LAX 23:43 JFK 02:43
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86