解析请求
1.这次我爬取的是微博视频,微博视频地址为:https://weibo.com/tv/show/1034:4559626352394245?from=old_pc_videoshow,其中请求json完整地址为:https://weibo.com/tv/api/component?page=/tv/show/1034:4559626352394245,如果你在浏览器中打开此链接会发现返回空值数据:
- {"code":"100001","msg":"miss param","data":{}}
2.通过F12打开开发者模式后过滤XHR并刷新页面可以看到访问json是通过POST方式请求并获得数据
3.Params url参数为:page: /tv/show/1034:4559626352394245,Data数据为:data: {"Component_Play_Playinfo":{"oid":"1034:4559626352394245"}}
4.注意请求头中有个referer,需要在headers里面传递此值,不然返回结果为空,此参数应该是防盗链~
5.此实例中POST请求需要用到几个关键:referer、params、data
爬虫代码
- import requests
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:81.0) Gecko/20100101 Firefox/81.0',
- 'Cookie': 'SUB=_2AkMo3ID4f8PxqwJRmPgUym_qb4p1yw3EieKegHEjJRMxHRl-yT9kqm8ltRB6A1yuF6bTm13hixhiGCyHGh9DfTSbBQyR; SUBP=0033WrSXqPxfM72-Ws9jqgMF55529P9D9WFl5JwYq1qEbl6Q9fZM4RXU; SINAGLOBAL=9248434776033.582.1602228176273; ULV=1602636912439:5:5:3:6951972941241.892.1602636911832:1602571579741; YF-V-WEIBO-G0=b09171a17b2b5a470c42e2f713edace0; _s_tentry=-; Apache=6951972941241.892.1602636911832; XSRF-TOKEN=1EJvg-4VlL0JOdTxmuA-dk5G',
- 'Referer': 'https://weibo.com/tv/show/1034:4559626352394245?from=old_pc_videoshow'
- }
- params = {
- 'page': '/tv/show/1034:4559626352394245'
- }
- data = {
- 'data': '{"Component_Play_Relatevideolist":{"next_cursor":2,"mid":4559672667278442}}'
- }
- res = requests.post('https://weibo.com/tv/api/component', headers=headers, params=params, data=data).json()
- print(res)
其中params里的数据是补全请求url后面的参数/tv/show/1034:4559626352394245,补全后的完整链接就是请求的url地址:https://weibo.com/tv/api/component?page=/tv/show/1034:4559626352394245
响应结果
- {
- "code": "100000",
- "msg": "succ",
- "data": {
- "Component_Play_Playinfo": {
- "mid": 4559672667278442,
- "id": "1034:4559626352394245",
- "oid": "1034:4559626352394245",
- "media_id": 4559626352394245,
- "user": {
- "id": 6414205745
- },
- "is_follow": true,
- "attitude": false,
- "date": "昨天",
- "idstr": "1034:4559626352394245",
- "author": "回形针PaperClip",
- "nickname": "回形针PaperClip",
- "verified": true,
- "verified_type": 0,
- "verified_type_ext": 1,
- "verified_reason": "泛科普视频自媒体 微博签约自媒体",
- "avatar": "//tvax1.sinaimg.cn/small/00705lVnly8fm007um5jdj31jk1jk420.jpg?KID=imgbed,tva&Expires=1602647374&ssig=kyIqBtW2Bw",
- "followers_count": "438万",
- "reposts_count": "1,146",
- "comments_count": "365",
- "attitudes_count": 1652,
- "title": "Vol.139【如何精确控制母鸡批量下蛋?】",
- "urls": {
- "高清 1080P+": "//f.video.weibocdn.com/Hqmql79Zlx07Hb7FExqo01041207ImN20E030.mp4?label=mp4_1080p%2B&template=1920x1080.20.0&trans_finger=9b069c34677b51372283af1a7d9a6c3c&media_id=4559626352394245&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=3&ot=h&ps=3lckmu&uid=2BzXHD&ab=966-g1&Expires=1602640174&ssig=x4LcDK9nXs&KID=unistore,video",
- "高清 1080P": "//f.video.weibocdn.com/l1G2UnWOlx07Hb7sZ2c801041204xHP20E020.mp4?label=mp4_1080p&template=1920x1080.25.0&trans_finger=d88af6227b88881484d2b59dfbafa136&media_id=4559626352394245&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=3&ot=h&ps=3lckmu&uid=2BzXHD&ab=966-g1&Expires=1602640174&ssig=i%2F3A8R5noQ&KID=unistore,video",
- "高清 720P": "//f.video.weibocdn.com/Zoc6EYLVlx07Hb7qvY2s01041202knLV0E010.mp4?label=mp4_720p&template=1280x720.25.0&trans_finger=775cb0ab963a74099cf9f840cd1987f1&media_id=4559626352394245&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=3&ot=h&ps=3lckmu&uid=2BzXHD&ab=966-g1&Expires=1602640174&ssig=jsD8Zj9M%2FD&KID=unistore,video",
- "标清 480P": "//f.video.weibocdn.com/fDYNN2Aclx07Hb7phhVK01041201c7Ys0E010.mp4?label=mp4_hd&template=852x480.25.0&trans_finger=d8257cc71422c9ad30fe69ce9523c87b&media_id=4559626352394245&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=3&ot=h&ps=3lckmu&uid=2BzXHD&ab=966-g1&Expires=1602640174&ssig=Cvva1sCujT&KID=unistore,video",
- "流畅 360P": "//f.video.weibocdn.com/6gaRTrhplx07Hb7p05fO01041200KXvn0E010.mp4?label=mp4_ld&template=640x360.25.0&trans_finger=6006a648d0db83b7d9951b3cee381a9c&media_id=4559626352394245&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=3&ot=h&ps=3lckmu&uid=2BzXHD&ab=966-g1&Expires=1602640174&ssig=nxbhh0bhCW&KID=unistore,video"
- },
- "cover_image": "//wx3.sinaimg.cn/crop.0.6.1920.1067/00705lVnly1gjnuzps8faj31hc0u0nohwebp",
- "duration": "5:46",
- "duration_time": 346.816,
- "play_start": 0,
- "play_start_time": 4,
- "play_count": "56.2万",
- "topics": [
- {
- "content": "微博公开课"
- }
- ],
- "uuid": "4559626489299325",
- "text": "Vol.139【如何精确控制母鸡批量下蛋?】\n\n一只蛋鸡的一生都经历了什么?怎样让十几万只鸡在同一时段下出外观、大小、颜色、口味等都相差无几的蛋?<a target=\"_blank\" href=\"//s.weibo.com/weibo?q=%23%E5%BE%AE%E5%8D%9A%E5%85%AC%E5%BC%80%E8%AF%BE%23&from=default\">#微博公开课#</a> \n\n*关注我们,在私信回复「鸡蛋」,可获取视频里提到的海兰褐父母代蛋鸡饲养手册。 ",
- "url_short": "http://t.cn/A6bKLLfn",
- "is_show_bulletin": 2,
- "comment_manage_info": {
- "comment_permission_type": -1,
- "approval_comment_type": 0
- },
- "video_orientation": "horizontal",
- "is_contribution": 1,
- "live": false,
- "scrubber": {
- "width": 160,
- "height": 90,
- "col": 10,
- "row": 10,
- "interval": 2,
- "urls": [
- "//wx4.sinaimg.cn/large/00705lVnly1gjo0aga8q0j318g0p0q6pwebp",
- "//wx4.sinaimg.cn/large/00705lVnly1gjo0ag9xxkj318g0p0mznwebp"
- ]
- },
- "user_video_count": 86,
- "allow_comment": true
- }
- }
- }