马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 hello1974 于 2021-4-27 19:24 编辑
初学python,requests.post().json(),总是出错,没法运行,请教应该怎么弄啊?
import requests
import json
#1.获取URL
url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsLis'
#2.UA伪装
headers = {
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
'Content-Type':'application/json'
}
#3.封装参数
data = {
'on':'true',
'page':'1',
'pageSize':'15',
'productName':'',
'conditionType':'1',
'applyname':'',
'applysn':'',
}
idlist = []#存储企业ID
all_data_list = []#存储所有企业详情数据
json_ids = requests.post(url=url,headers=headers,data=data).json()
for dic in jsonids['list']:
idlist.append(dic['id'])
#获取企业详情数据
post_url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById'
for id in id_list:
data = {
'id':id
}
detail_json = requests.post(url=post_url,headers=headers,data=data).json()
all_data_list.append(detail_json)
#持久化存储
fp = open('alldata.json','w',encoding='utf-8')
json.dump(all_data_list,fp=fp,ensure_ascii=False)
print('over!!!')
|