forked from xiyaowong/spiders
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkuaishou.py
More file actions
64 lines (55 loc) · 1.89 KB
/
kuaishou.py
File metadata and controls
64 lines (55 loc) · 1.89 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import json
import re
import requests
def get(url: str) -> dict:
"""
title、imgs、videos
"""
data = {}
failed = {'msg': 'failed...'}
headers = {
"User-Agent": "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25",
"Cookie": "did=web_68e0268146694843a92700d2de49a0a6;"
}
# rewrite desktop url
temp = re.findall(r'live\.kuaishou\.com/u/\w+/(\w+)', url)
if temp:
url = 'https://c.kuaishou.com/fw/photo/{}'.format(temp[0])
rep = requests.get(url, headers=headers, timeout=10)
if rep.status_code != 200:
return failed
page_data = re.findall(r'<script type="text/javascript">window\.pageData= (\{.*?\})</script>', rep.text)
if not page_data:
return failed
try:
page_data = json.loads(page_data[0])
except Exception:
print('kuaishou loads json failed')
return failed
video_info = page_data['video']
data['title'] = video_info['caption']
# 获取视频
try: # 如果出错,则可能是长图视频
data['videos'] = [video_info['srcNoMark']]
except Exception:
pass
else:
data['videoName'] = data['title']
data['msg'] = '如果快手视频下载出错请尝试更换网络'
# 获取图片
try: # 如果出错,则可能是普通视频;
images = video_info['images']
imageCDN: str = video_info['imageCDN']
# 如果是长图视频,则这几项一定存在
assert images is not None
assert imageCDN is not None
except Exception:
pass
else:
if not imageCDN.startswith('http'):
imageCDN = 'http://' + imageCDN
data['imgs'] = [imageCDN + i['path'] for i in images]
return data
if __name__ == "__main__":
from pprint import pprint
pprint(get(input("url: ")))