aiohttp_requests库的学习 json(content_type='??')

from aiohttp_requests import requests

async def req(URL):
    headers = {"Accept": 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        "Accept-Language": "zh-CN,zh;q=0.9", "Host": "xin.baidu.com",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", }
    resp = await requests.get(URL,headers=headers,timeout=10,verify_ssl=False)
    resp_text = await resp.json(encoding='utf-8')
    return resp_text

if __name__ == "__main__":
    print("HELLO WORLD")
    URL = "https://xin.baidu.com/detail/basicAjax?pid=xlTM-TogKuTw9PzC-u6VwZxUBuZ5J7WMewmd"
    loop = asyncio.get_event_loop()
    loop.run_until_complete(req(URL))

运行上面的程序会报错

aiohttp.client_exceptions.ContentTypeError: 0, message='Attempt to decode JSON with unexpected mimetype: text/html; charset=utf-8'

解决方案

from aiohttp_requests import requests

async def req(URL):
    headers = {"Accept": 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
        "Accept-Language": "zh-CN,zh;q=0.9", "Host": "xin.baidu.com",
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36", }
    resp = await requests.get(URL,headers=headers,timeout=10,verify_ssl=False)
    resp_text = await resp.json(content_type='text/html',encoding='utf-8')
    return resp_text

if __name__ == "__main__":
    print("HELLO WORLD")
    URL = "https://xin.baidu.com/detail/basicAjax?pid=xlTM-TogKuTw9PzC-u6VwZxUBuZ5J7WMewmd"
    loop = asyncio.get_event_loop()
    loop.run_until_complete(req(URL))

 

你可能感兴趣的:(aiohttp_requests库的学习 json(content_type='??'))