py小bai 2021-06-25 09:46 采纳率: 81.3%
浏览 83
已采纳

python爬虫报错,jsonp

描述:爬取动态加载的网站,目标url存在于js中,用jsonp来封装的; 问题:直接爬取可以获得text,但是用json解析就报错,如下 Traceback (most recent call last): File "C:\Users\zhaorl\Desktop\python\0525上午.py", line 20, in res=json.loads(str(res)) File "C:\Python\lib\json_init_.py", line 346, in loads return _default_decoder.decode(s) File "C:\Python\lib\json\decoder.py", line 337, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "C:\Python\lib\json\decoder.py", line 355, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

爬取代码: import requests import json headers={ 'Host': 'xyq.cbg.163.com', 'Referer': 'https://xyq.cbg.163.com/cgi-bin/xyq_overall_search.py?act=show_overall_search_yuanshen', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36' } params={ 'callback': 'Request.JSONP.request_map.request_0', '_': '1624582691785', 'act': 'recommd_by_role', 'page': '1', 'attr_type': 'zongshang', 'count': '15', 'search_type': 'overall_search_yuanshen', 'view_loc': 'overall_search' } url='https://xyq.cbg.163.com/cgi-bin/recommend.py' res=requests.get(url=url,headers=headers,params=params) res=json.loads(str(res)) print(res.text)

  • 写回答

3条回答 默认 最新

  • 江天暮雪丨 2021-06-25 17:43
    关注

    参数callback去掉

    import requests
    import json
    
    headers = {'Host': 'xyq.cbg.163.com',
               'Referer': 'https://xyq.cbg.163.com/cgi-bin/xyq_overall_search.py?act=show_overall_search_yuanshen',
               'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
                             'Chrome/72.0.3626.121 Safari/537.36'}
    # params = {'callback': 'Request.JSONP.request_map.request_0', '_': '1624582691785', 'act': 'recommd_by_role',
    #           'page': '1', 'attr_type': 'zongshang', 'count': '15', 'search_type': 'overall_search_yuanshen',
    #           'view_loc': 'overall_search'}
    
    params = {'_': '1624582691785', 'act': 'recommd_by_role',
              'page': '1', 'attr_type': 'zongshang', 'count': '15', 'search_type': 'overall_search_yuanshen',
              'view_loc': 'overall_search'}
    url = 'https://xyq.cbg.163.com/cgi-bin/recommend.py'
    res = requests.get(url=url, headers=headers, params=params).json()   #  此处添加.json()
    # res = json.loads(res)
    print(res)
    
    # result:
    # {'msg': '请输入验证码!', 'status': 3, 'server_time': 1624614077246}
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 soildworks装配体的尺寸问题
  • ¥100 有偿寻云闪付SDK转URL技术
  • ¥30 基于信创PC发布的QT应用如何跨用户启动后输入中文
  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑