请麻烦帮我看一下“for li in res['result']['data']:”循环遍历。
报错“'NoneType' object is not subscriptable“
def collect():
url = 'https://datacenter-web.eastmoney.com/api/data/v1/get'
r = requests.get(url)
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36'}
params = {
'sortColumns' : ['LISTING_DATE','SECURITY_CODE'],
'sortTypes' : ['-1', ' -1'],
'pageSize': '10000',
'pageNumber' : '1',
'reportName' : 'RPTA_APP_IPOAPPLY',
'quoteColumns': ['f2~01~SECURITY_CODE','f14~01~SECURITY_CODE'],
'quoteType': '0',
'columns': 'ALL',
'source': 'WEB',
'client': 'WEB',
'filter': f'''((APPLY_DATE>'2010-01-01')(|@APPLY_DATE='NULL'))((LISTING_DATE>'2010-01-01')(|@LISTING_DATE='NULL'))(TRADE_MARKET_CODE!'069001017')'''
#日期这个问题先不解决了吧
}
res = requests.get(url = url, headers = headers, params= params).json()
for li in res['result']['data']:
secid= '0'+'.' + li['SECUCODE'][:6] if li['SECUCODE'][-2:] == 'SZ' else '1'+'.' + li['SECUCODE'][:6]
urlLi = 'https://push2.eastmoney.com/api/qt/stock/get'
paramsLi ={
'fltt': '2',
'invt': '2',
'fields': 'f78,f58,f59,f86,f43,f169,f170,f44,f45,f46,f47,f116,f117,f162,f167,f60,f137,f469,f434,f470,f459,f471,f292',
'secid' : secid
}
resLi = requests.get(url = urlLi, headers = headers, params= paramsLi).json()
dic = {
'序号' : res['result']['data'].index(li) + 1,
'股票代码' : li['SECURITY_CODE'],
'股票简称' : li['SECURITY_NAME'],
'发行价' : li['ISSUE_PRICE'],
'最新价' : resLi['data']['f43'],
'网上发⾏中签率(%)' : li['ONLINE_ISSUE_LWR'],
'网上有效申购股数(万股)' : li['ONLINE_VA_SHARES'],
'网上有效申购户数(户)' : li['ONLINE_VA_NUM'],
'⽹上超额认购' : li['ONLINE_ES_MULTIPLE'],
'线下配售中签率(%)' : li['OFFLINE_VAP_RATIO'],
'线下有效申购股数(万股)' : li['OFFLINE_VATS'],
'线下有效申购户数(户)' : li['OFFLINE_VAP_OBJECT'],
'线下配售认购倍数' : li['OFFLINE_VAS_MULTIPLE'],
'总发⾏数量(万股)' : li['TOTAL_ISSUE_NUM'],
'开盘溢价(%)' : li['LD_OPEN_PREMIUM'],
'⾸⽇涨幅(%)' : li['LD_CLOSE_CHANGE'],
}
print(dic)
csv.writer(open('IPO Earning.csv','a',encoding = 'utf-8-sig', newline='')).writerow(dic.values())
if __name__ == '__main__':
csv.writer(open('IPO Earning.csv','a',encoding = 'utf-8-sig', newline='')).writerow(['序号','股票代码', '股票简称', '发行价' , '最新价', '网上发⾏中签率(%)', '网上有效申购股数(万股)','网上有效申购户数(户)','⽹上超额认购', '线下配售中签率(%)','线下有效申购股数(万股)','线下有效申购户数(户)','线下配售认购倍数', '总发⾏数量(万股)','开盘溢价(%)', '⾸⽇涨幅(%)'])
collect()