学习链接b站:https://www.bilibili.com/video/BV1Yh411o7Sz?p=11
import requests
import json
url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList'
# 获取id信息
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'}
ID_list = [] # 存放id 的列表
def get_idlist():
# 请求主页面
pages = 5
for page in range(pages):
params = {
'on': 'true',
'page': str(page),
'pageSize': '15',
'productName': '',
'conditionType': '1',
'applyname': '',
'applysn': '',
}
re = requests.post(url, params=params, headers=headers)
# 将response转化为字典类型
response_dic = json.loads(re.text)
# 把 id 放入列表中
for item in response_dic['list']:
ID_list.append(item['ID'])
def writeinfo():
for aid in ID_list:
url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById'
params = {'id': aid}
get_info = requests.post(url, params=params, headers=headers)
with open('./info.txt', 'a+', encoding='utf-8') as fp:
fp.write(get_info.text)
fp.writelines("\n")
get_idlist()
writeinfo()