鱼C论坛

 找回密码
 立即注册
查看: 1100|回复: 4

关于json.loads()的问题

[复制链接]
发表于 2019-6-20 13:30:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
为什么同一行代码运行的结果每次都不一样,但是出错都是这个原因
代码是这样的
while Ture:
        
        url="http://www.nffund.com/QueryFundNoticeServlet?fundcode={0}&infoType=&type=0&title=&pageNo={1}&pageSize=15".format(fundcode,pageNo)
        print(url)
        headers  = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'}
        response = requests.get(url=url,headers=headers)
        data     = json.loads(response.text)

运行结果有:
http://www.nffund.com/QueryFundN ... 1&pageSize=1000
Traceback (most recent call last):
  File "C:\Users\lenovo\Desktop\111.py", line 146, in <module>
    get_the_fund_list(address_in,address_out)
  File "C:\Users\lenovo\Desktop\111.py", line 24, in get_the_fund_list
    DF1=get_the_Fund_Bulletin_address(fund)
  File "C:\Users\lenovo\Desktop\111.py", line 54, in get_the_Fund_Bulletin_address
    data     = json.loads(response.text)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\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)
>>>
================== RESTART: C:\Users\lenovo\Desktop\111.py ==================
http://www.nffund.com/QueryFundN ... o=1&pageSize=15
http://www.nffund.com/QueryFundN ... o=2&pageSize=15
http://www.nffund.com/QueryFundN ... o=3&pageSize=15
http://www.nffund.com/QueryFundN ... o=4&pageSize=15
http://www.nffund.com/QueryFundN ... o=5&pageSize=15
Traceback (most recent call last):
  File "C:\Users\lenovo\Desktop\111.py", line 146, in <module>
    get_the_fund_list(address_in,address_out)
  File "C:\Users\lenovo\Desktop\111.py", line 24, in get_the_fund_list
    DF1=get_the_Fund_Bulletin_address(fund)
  File "C:\Users\lenovo\Desktop\111.py", line 54, in get_the_Fund_Bulletin_address
    data     = json.loads(response.text)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\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)
>>>
================== RESTART: C:\Users\lenovo\Desktop\111.py ==================
http://www.nffund.com/QueryFundN ... o=1&pageSize=15
Traceback (most recent call last):
  File "C:\Users\lenovo\Desktop\111.py", line 146, in <module>
    get_the_fund_list(address_in,address_out)
  File "C:\Users\lenovo\Desktop\111.py", line 24, in get_the_fund_list
    DF1=get_the_Fund_Bulletin_address(fund)
  File "C:\Users\lenovo\Desktop\111.py", line 54, in get_the_Fund_Bulletin_address
    data     = json.loads(response.text)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\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)
>>>
================== RESTART: C:\Users\lenovo\Desktop\111.py ==================
http://www.nffund.com/QueryFundN ... o=1&pageSize=15
Traceback (most recent call last):
  File "C:\Users\lenovo\Desktop\111.py", line 146, in <module>
    get_the_fund_list(address_in,address_out)
  File "C:\Users\lenovo\Desktop\111.py", line 24, in get_the_fund_list
    DF1=get_the_Fund_Bulletin_address(fund)
  File "C:\Users\lenovo\Desktop\111.py", line 54, in get_the_Fund_Bulletin_address
    data     = json.loads(response.text)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\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)
>>>
================== RESTART: C:\Users\lenovo\Desktop\111.py ==================
http://www.nffund.com/QueryFundN ... o=1&pageSize=15
http://www.nffund.com/QueryFundN ... o=2&pageSize=15
http://www.nffund.com/QueryFundN ... o=3&pageSize=15
http://www.nffund.com/QueryFundN ... o=4&pageSize=15
Traceback (most recent call last):
  File "C:\Users\lenovo\Desktop\111.py", line 146, in <module>
    get_the_fund_list(address_in,address_out)
  File "C:\Users\lenovo\Desktop\111.py", line 24, in get_the_fund_list
    DF1=get_the_Fund_Bulletin_address(fund)
  File "C:\Users\lenovo\Desktop\111.py", line 54, in get_the_Fund_Bulletin_address
    data     = json.loads(response.text)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\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)
>>>
================== RESTART: C:\Users\lenovo\Desktop\111.py ==================
http://www.nffund.com/QueryFundN ... o=1&pageSize=15
http://www.nffund.com/QueryFundN ... o=2&pageSize=15
Traceback (most recent call last):
  File "C:\Users\lenovo\Desktop\111.py", line 146, in <module>
    get_the_fund_list(address_in,address_out)
  File "C:\Users\lenovo\Desktop\111.py", line 24, in get_the_fund_list
    DF1=get_the_Fund_Bulletin_address(fund)
  File "C:\Users\lenovo\Desktop\111.py", line 54, in get_the_Fund_Bulletin_address
    data     = json.loads(response.text)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Users\lenovo\AppData\Local\Programs\Python\Python37\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)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-6-22 00:19:12 | 显示全部楼层
本帖最后由 快速收敛 于 2019-6-22 00:30 编辑

请求头参数不全,被反爬了,获取的并不是json字符串,因此无法解码为python对象,换成我这个请求头试试
  1. headers  = {
  2.     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36',
  3.     "Host": "www.nffund.com"
  4. }
复制代码

如果可行,给个最佳答案,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-6-22 23:26:50 | 显示全部楼层
你先print一下你的response是什么内容,到底是不是200。
然后再print一下你的response.text是什么内容。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-8-7 14:58:32 | 显示全部楼层
快速收敛 发表于 2019-6-22 00:19
请求头参数不全,被反爬了,获取的并不是json字符串,因此无法解码为python对象,换成我这个请求头试试

...

试了一下,比之前爬多了几页,但之后获取的又不是json字符串
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-8-8 08:14:26 | 显示全部楼层
与道相融 发表于 2019-8-7 14:58
试了一下,比之前爬多了几页,但之后获取的又不是json字符串

逐个检查,安装
  1. faker_useragent
复制代码
库,模拟更多的User-Agent,试试
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-1-16 11:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表