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

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 关于龙芯1b,JTAG停止调试服务
  • ¥20 收一个快手协议下单算法
  • ¥15 神途脚本逐鹿神途,管理自欺欺人
  • ¥15 求一个图片中的成交量选股公式
  • ¥15 已知正方形内随机生成坐标matlab
  • ¥30 关于#python#的问题:我想要的是这79个大特征对于房屋售价的最大的影响前十名(相关搜索:随机森林)
  • ¥15 react native新项目运行时遇到问题
  • ¥15 脚本运行Python文件,报错:error: unrecognized
  • ¥15 使用matlab计算自定义特殊函数的二重积分,改变积分顺序所得的结果不同的问题?
  • ¥15 如何正确在vs2010中初始化map对象