python爬虫初学者小实例

  • 时间:
  • 浏览:
  • 来源:互联网

python爬虫初学者小实例

 1. # -*- coding: utf-8 -*- """ Created on Fri Dec 18 11:43:48 2020
    
    @author: Lenovo """
    import requests
    import json
    if __name__ == '__main__':
        #1.指定url 
        myurl = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList'
        # 封装参数
        param = {
                    'on':'true',
                    'page':'1',
                    'pageSize':'15',
                   ' productName':'',
                    'conditionType':'1',
                    'applyname':'',
                    'applysn':''
        }
        
        # 请求头
        headers = { 
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.75
    Safari/537.36'
        }
        
        #2.发起请求 post返回一个响应对象
        response = requests.post(url=myurl,data=param,headers = headers)
        # 3.获取相应数据   
        list_ids=response.json()
        #存放所有id
        ids=[]
        # 4.持久化存储
        for dic in list_ids['list']:
            ids.append(dic['ID'])
        detail_url='http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById'
        
        for id in ids:
            detail_param={'id':id}
            detail_json =requests.post(url=detail_url, data=detail_param,headers=headers).json()
            # print(detail_json)
        print('爬取完毕')

本文链接http://www.hatan.cn/news/show-69540.html