python提示jsondecodeerror是什么意思_json.decoder.JSONDecodeError

import requests

import re

import json

import csv

#定义一个get_table()方法来输出抓取的第一页表格内容

def get_table():

params = {      #params为url请求中所包含的参数

'type':'CWBB_LRB20',#表格类型,LRB为利润表缩写,必须

'token':'70f12f2f4f091e459a279469fe49eca5',#访问令牌,必须

'st':'noticedate',

'sr':-1,

'p':1,

'ps':50,

'js':'var YlIfdEKv={pages:(tp),data: (x),font:(font)}',

'filter':"(securitytypecode='058001001')(reportdate=^2018-06-30^)"#筛选条件

#'rt':52961251  可以不用

}

#params参数设置好之后,将url和params参数一起传进requests.get()方法中,这样就构造好了请求连接

url='http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get?type=CWBB_LRB20&token=70f12f2f4f091e459a279469fe49eca5&st=noticedate&sr=-1&p=2&ps=50&js=var%20svbCesOb={pages:(tp),data:%20(x),font:(font)}&filter=(securitytypecode=%27058001001%27)(reportdate=^2018-06-30^)&rt=52961271'

#仅使用http://dcfm.eastmoney.com/em_mutisvcexpandinterface/api/js/get?搜索出来的页面只显示hello world ,用了这么长的才能搜索出来显示的是数据

response = requests.get(url,params=params).text

#print(response)

#get_table()

#正则表达式提取表格

#确定页数

page_all = re.search(r'var.*?{pages:(\d+),data:.*?',response) #用\d+匹配页数中的数值

#page_all=re.search(pat,response) #用re.search()方法提取出前面匹配的数值

#print(type(page_all))

print(page_all.group(1)) #group(1)表示输出第一个结果,这里就是()中的页数

#提取出list,可以用json.dumps和json.loads

#pattern = re.compile(r'var.*?data:(.*)}',re.S)

items = re.search(r'var.*?data:(.*)}',response) #re.search()返回的结果是字符串类型

data=items.group(1)

#print(type(data)) #注释掉的是用于检测类型

#print(type(list(data)))

data=data[0]

#print(type(data))

#print(data[0])

for d in data:

with open('eastmoney.csv','a',encoding='utf-8',newline='') as f:

w = csv.writer(f)

w.writerow(d.values)

try:

data=json.loads(data)

except json.decoder.JSONDecodeError as reason:

print('九千岁',reason)

print(type(data))

print(data[0])

for d in data:

with open('eastmoney.csv','a',encoding='utf-8',newline='') as f:

w = csv.writer(f)

w.writerow(d.values)

get_table()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值