• 简单爬虫案例——爬取快手视频


    网址:aHR0cHM6Ly93d3cua3VhaXNob3UuY29tL3NlYXJjaC92aWRlbz9zZWFyY2hLZXk9JUU2JThCJTg5JUU5JTlEJUEy

    找到视频接口

    视频链接在photourl中

     

    完整代码:

    1. import requests
    2. import re
    3. url = 'https://www.kuaishou.com/graphql'
    4. cookies = {
    5. 'did': 'web_9e8cfa4403000587b9e7d67233e6b04c',
    6. 'didv': '1719811812378',
    7. 'kpf': 'PC_WEB',
    8. 'clientid': '3',
    9. 'kpn': 'KUAISHOU_VISION',
    10. }
    11. headers = {
    12. 'Accept-Language': 'zh-CN,zh;q=0.9',
    13. 'Cache-Control': 'no-cache',
    14. 'Connection': 'keep-alive',
    15. # 'Cookie': 'did=web_9e8cfa4403000587b9e7d67233e6b04c; didv=1719811812378; kpf=PC_WEB; clientid=3; kpn=KUAISHOU_VISION',
    16. 'Origin': 'https://www.kuaishou.com',
    17. 'Pragma': 'no-cache',
    18. 'Referer': 'https://www.kuaishou.com/search/video?searchKey=%E6%8B%89%E9%9D%A2',
    19. 'Sec-Fetch-Dest': 'empty',
    20. 'Sec-Fetch-Mode': 'cors',
    21. 'Sec-Fetch-Site': 'same-origin',
    22. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36',
    23. 'accept': '*/*',
    24. 'content-type': 'application/json',
    25. 'sec-ch-ua': '"Not/A)Brand";v="8", "Chromium";v="126", "Google Chrome";v="126"',
    26. 'sec-ch-ua-mobile': '?0',
    27. 'sec-ch-ua-platform': '"Windows"',
    28. }
    29. json_data = {
    30. 'operationName': 'visionSearchPhoto',
    31. 'variables': {
    32. 'keyword': '拉面',
    33. 'pcursor': '',
    34. 'page': 'search',
    35. },
    36. 'query': 'fragment photoContent on PhotoEntity {\n __typename\n id\n duration\n caption\n originCaption\n likeCount\n viewCount\n commentCount\n realLikeCount\n coverUrl\n photoUrl\n photoH265Url\n manifest\n manifestH265\n videoResource\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n profileUserTopPhoto\n musicBlocked\n riskTagContent\n riskTagUrl\n}\n\nfragment recoPhotoFragment on recoPhotoEntity {\n __typename\n id\n duration\n caption\n originCaption\n likeCount\n viewCount\n commentCount\n realLikeCount\n coverUrl\n photoUrl\n photoH265Url\n manifest\n manifestH265\n videoResource\n coverUrls {\n url\n __typename\n }\n timestamp\n expTag\n animatedCoverUrl\n distance\n videoRatio\n liked\n stereoType\n profileUserTopPhoto\n musicBlocked\n riskTagContent\n riskTagUrl\n}\n\nfragment feedContent on Feed {\n type\n author {\n id\n name\n headerUrl\n following\n headerUrls {\n url\n __typename\n }\n __typename\n }\n photo {\n ...photoContent\n ...recoPhotoFragment\n __typename\n }\n canAddComment\n llsid\n status\n currentPcursor\n tags {\n type\n name\n __typename\n }\n __typename\n}\n\nquery visionSearchPhoto($keyword: String, $pcursor: String, $searchSessionId: String, $page: String, $webPageArea: String) {\n visionSearchPhoto(keyword: $keyword, pcursor: $pcursor, searchSessionId: $searchSessionId, page: $page, webPageArea: $webPageArea) {\n result\n llsid\n webPageArea\n feeds {\n ...feedContent\n __typename\n }\n searchSessionId\n pcursor\n aladdinBanner {\n imgUrl\n link\n __typename\n }\n __typename\n }\n}\n',
    37. }
    38. response = requests.post(url=url, cookies=cookies, headers=headers, json=json_data)
    39. for index in response.json()['data']['visionSearchPhoto']['feeds']:
    40. title = index['photo']['caption']
    41. newtitle = re.sub(r'[\\/?<>:*|\n\r]','',title)
    42. link = index['photo']['photoUrl']
    43. print(title,link)
    44. content = requests.get(url=link,headers=headers).content
    45. with open('快手video//'+title+'.mp4','wb') as f:
    46. f.write(content)

    结果展现:

     

     

  • 相关阅读:
    数仓建模分层理论
    JavaScript-Object.defineProperty函数
    Lua脚本
    EdrawMax思维导图,EdrawMax组织结构图
    Java Springboot项⽬部署到linux任意文件夹或tomcat,并使用nginx实现域名访问
    深入理解vue2.x双向数据绑定原理
    【微客云】提供三网92折话费充值API接口稳定到账独立后台售后服务(帮你对接小程序app)
    EfficientNet代码复现--ICML2019
    Shell基础— Bash的基本功能(二)
    探花交友_第1章_项目介绍以及实现登录功能_第3节_实现登录功能
  • 原文地址:https://blog.csdn.net/m0_57265868/article/details/140098714