Parsing HTTP Response in Python

后端 未结 5 716
夕颜
夕颜 2020-12-13 09:08

I want to manipulate the information at THIS url. I can successfully open it and read its contents. But what I really want to do is throw out all the stuff I don\'t want,

5条回答
  •  粉色の甜心
    2020-12-13 09:47

    json works with Unicode text in Python 3 (JSON format itself is defined only in terms of Unicode text) and therefore you need to decode bytes received in HTTP response. r.headers.get_content_charset('utf-8') gets your the character encoding:

    #!/usr/bin/env python3
    import io
    import json
    from urllib.request import urlopen
    
    with urlopen('https://httpbin.org/get') as r, \
         io.TextIOWrapper(r, encoding=r.headers.get_content_charset('utf-8')) as file:
        result = json.load(file)
    print(result['headers']['User-Agent'])
    

    It is not necessary to use io.TextIOWrapper here:

    #!/usr/bin/env python3
    import json
    from urllib.request import urlopen
    
    with urlopen('https://httpbin.org/get') as r:
        result = json.loads(r.read().decode(r.headers.get_content_charset('utf-8')))
    print(result['headers']['User-Agent'])
    

提交回复
热议问题