Python学习笔记:Python3中Json数据遍历取指定值

记录在爬取微博博主微博数据时使用Python3中Json数据遍历取指定值

json地址

请求的json地址为:https://m.weibo.cn/api/container/getIndex?type=uid&value=6414205745&containerid=1076036414205745

遍历数据

遍历json数据中“data”“cards”“mblog”“page_info”“media_info”下的视频链接地址,即“stream_url”中的http://f.video.weibocdn.com/7zCpXkfllx07CUfbom0001041201UmjF0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=40a32e8439c5409a63ccf853562a60ef&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602643858&ssig=U2pxNFDptM&KID=unistore,video

Python代码

  1. import requests  
  2.   
  3. headers = {  
  4.     ‘User-Agent’: ‘Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1’,  
  5.     ‘Cookie‘: ‘_T_WM=78921707454; XSRF-TOKEN=fe3a88; WEIBOCN_FROM=1110003030; MLOGIN=0; M_WEIBOCN_PARAMS=luicode%3D10000011%26lfid%3D1076036414205745%26fid%3D1005056414205745%26uicode%3D10000011’,  
  6. }  
  7. params ={  
  8.     ‘type‘: ‘uid’,  
  9.     ‘value’: 6414205745,  
  10.     ‘containerid’: 1076036414205745  
  11. }  
  12. res = requests.get(‘https://m.weibo.cn/api/container/getIndex’, headers=headers, params=params).json()  
  13. for item in res[‘data’][‘cards’]:  
  14.     try:  
  15.         if ‘retweeted_status’ in item[‘mblog’].keys():  
  16.             print(item[‘mblog’][‘retweeted_status’][‘page_info’][‘media_info’][‘stream_url’])  
  17.         else:  
  18.             print(item[‘mblog’][‘page_info’][‘media_info’][‘stream_url’])  
  19.     except Exception as e:  
  20.         pass  

代码说明

1.import requests 即引入requests请求库

  1. import requests  

2.headers即构造爬虫请求头

  1. headers = {  
  2.     ‘User-Agent’: ‘Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1’,  
  3.     ‘Cookie‘: ‘_T_WM=78921707454; XSRF-TOKEN=fe3a88; WEIBOCN_FROM=1110003030; MLOGIN=0; M_WEIBOCN_PARAMS=luicode%3D10000011%26lfid%3D1076036414205745%26fid%3D1005056414205745%26uicode%3D10000011’,  
  4. }  

3.params即url参数传递

  1. params ={  
  2.     ‘type‘: ‘uid’,  
  3.     ‘value’: 6414205745,  
  4.     ‘containerid’: 1076036414205745  
  5. }  

4.res即请求后返回的json数据

  1. res = requests.get(‘https://m.weibo.cn/api/container/getIndex’, headers=headers, params=params).json()  

5.for item in res[‘data’][‘cards’]:即在函数为res的json数据[‘data’][‘cards’]目录下循环
6.判断[‘mblog’]里是否包含retweeted_status,如果包含返回[‘mblog’][‘retweeted_status’][‘page_info’][‘media_info’]下stream_url对应值

  1. if ‘retweeted_status’ in item[‘mblog’].keys():  
  2.     print(item[‘mblog’][‘retweeted_status’][‘page_info’][‘media_info’][‘stream_url’])  

7.如果不包含retweeted_status,则返回[‘mblog’][‘page_info’][‘media_info’]下的stream_url对应值

  1. else:  
  2.     print(item[‘mblog’][‘page_info’][‘media_info’][‘stream_url’])  

运行结果

  1. http://f.video.weibocdn.com/7zCpXkfllx07CUfbom0001041201UmjF0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=40a32e8439c5409a63ccf853562a60ef&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=d1ZuPIEVDE&KID=unistore,video  
  2. http://f.video.weibocdn.com/6gaRTrhplx07Hb7p05fO01041200KXvn0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=6006a648d0db83b7d9951b3cee381a9c&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=bkDerNKeQF&KID=unistore,video  
  3. http://f.video.weibocdn.com/6gaRTrhplx07Hb7p05fO01041200KXvn0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=6006a648d0db83b7d9951b3cee381a9c&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=bkDerNKeQF&KID=unistore,video  
  4. http://f.video.weibocdn.com/6gaRTrhplx07Hb7p05fO01041200KXvn0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=6006a648d0db83b7d9951b3cee381a9c&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=bkDerNKeQF&KID=unistore,video  
  5. http://f.video.weibocdn.com/ClGEq7yclx07H8NkMC2Q0104120053fx0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=40a32e8439c5409a63ccf853562a60ef&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=lj%2FqbLLZ8r&KID=unistore,video  
  6. http://f.video.weibocdn.com/5M4bafcxlx07H6kUixWE01041201jIvC0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=6006a648d0db83b7d9951b3cee381a9c&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=S73r3lf5p3&KID=unistore,video  
  7. http://f.video.weibocdn.com/5M4bafcxlx07H6kUixWE01041201jIvC0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=6006a648d0db83b7d9951b3cee381a9c&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=S73r3lf5p3&KID=unistore,video  
  8. http://f.video.weibocdn.com/5M4bafcxlx07H6kUixWE01041201jIvC0E010.mp4?label=mp4_ld&template=640×360.25.0&trans_finger=6006a648d0db83b7d9951b3cee381a9c&ori=0&ps=1CwnkDw1GXwCQx&Expires=1602645297&ssig=S73r3lf5p3&KID=unistore,video  

给TA打赏
共{{data.count}}人
人已打赏
Python

Python学习笔记:爬取IVI测试测试频道列表

2020-9-25 13:11:57

Python

Python学习笔记:requests请求传递Query String Parameters参数及提交From Data数据

2020-10-14 9:37:19

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
搜索