requests基础2

爬虫之requests模块

如何捕获到动态加载的数据?

  • 基于抓包工具进行全局搜索
    • 在抓包工具中进行全局搜索
  • 基于抓包工具进行全局搜索不一定可以每次都能定位到动态加载数据对应的数据包。
    • 如果动态加载的数据是经过加密的密文数据,就会搜索不到对应的数据包。(加密解密js混淆)

-定位到动态加载数据对应的数据包,从该数据包中就可以提取出

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
url = 'https://movie.douban.com/j/chart/top_list'
params = {
    'type': '24',
    'interval_id': '100:90',
    'action':' ',
    'start': '0',
    'limit': '20'
}
response = requests.get(url=url,params=params,headers=headers)
#json()将获取的字符串形式的json数据序列化成字典或者列表对象
page_text = response.json()
for movie in page_text:
    name = movie['title']
    score = movie['score']
    print(name, score)

分页数据的爬取操作

  • 爬取肯德基的餐厅位置数据

    • url:http://www.kfc.com.cn/kfccda/storelist/index.aspx
  • 分析

    • 在录入关键字的文本框中录入关键字按下搜索按钮,发起的是一个ajax请求
      • 当前页面刷仙出来的位置信息一定是通过ajax请求请求到的数据
    • 基于抓包工具定位到该ajax请求的数据包,从该数据包中捕获到:
      1. 请求的url
      2. 请求方式
      3. 请求携带的参数
      4. 看到响应数据
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    
    #爬取单页
    url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
    data = {
        'cname': '',
        'pid': '',
        'keyword': '广州',
        'pageIndex': '1',
        'pageSize': '10',
    }
    
    response = requests.post(url=url,data=data,headers=headers)
    page_text = response.json()
    for canting in page_text['Table1']:
        title = canting['storeName']
        address = canting['addressDetail']
        print(title, address)
    
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    
    # 爬取多页
    for page in range(1, 3):
        url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
        data = {
            'cname': '',
            'pid': '',
            'keyword': '广州',
            'pageIndex': str(page),
            'pageSize': '10',
        }
    
        response = requests.post(url=url,data=data,headers=headers)
        page_text = response.json()
        for canting in page_text['Table1']:
            title = canting['storeName']
            address = canting['addressDetail']
            print(title, address)
    

    练习

    爬取国家药监局前三页企业信息:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    
    #药监局信息爬取
    for page in range(1, 4):
        url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsList'
        data = {
            'on': 'true',
            'page': str(page),
            'pageSize': '15',
            'productName': '',
            'conditionType': '1',
            'applyname': '',
             'applysn': '',
        }
    
        response = requests.post(url=url,data=data,headers=headers)
        page_text = response.json()
    
        for shuju in page_text['list']:
            ID = shuju['ID']
            url = 'http://scxk.nmpa.gov.cn:81/xk/itownet/portalAction.do?method=getXkzsById'
            data = {
                'id': str(ID)
                }
            response = requests.post(url=url, data=data, headers=headers)
            page_text1 = response.json()
    
            a = page_text1['epsName']
            b = page_text1['businessPerson']
            print(a, b)
    
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus