知乎想法抽奖专用

背景

很久没有更新专栏了,写一个好玩的把(刚好最近很多人有需求)
废话不多说,直接看代码把(接口是通过抓取手机app的包获取的,不会的话请看抓取手机app的数据(摩拜单车)

怎么用

  1. 打开想法详情页
  2. 点击复制链接
  3. python zhihu.py --url 刚刚复制的网址

代码如下

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
65
66
67
68
# coding: utf-8
import requests
from random import choice
import argparse
import re

# 模拟浏览器头
headers = {
'host': "api.zhihu.com",
'accept': "*/*",
'x-app-za': "OS=iOS&Release=10.3.3&Model=iPhone8,2&VersionName=4.1.0&VersionCode=683&Width=1242&Height=2208&DeviceType=Phone&Brand=Apple&OperatorType=46001",
'x-udid': "AHCAH2SzkApLBWvOUMZ7s9hG-Zh-_NhtVCM=",
'x-app-versioncode': "683",
'accept-language': "zh-Hans-CN;q=1, en-CN;q=0.9",
'accept-encoding': "gzip, deflate",
'x-api-version': "3.0.64",
'authorization': "Bearer gt2.0AAAAAAX1feoKkLNkH4BwAAAAAAtNVQJgAgBgPzk1-59vTbuXI60W0XlSl-wNBg==",
'x-network-type': "WiFi",
'user-agent': "osee2unifiedRelease/4.1.0 (iPhone; iOS 10.3.3; Scale/3.00)",
'x-app-build': "release",
'x-app-version': "4.1.0",
'x-suger': "SURGVj0wNDA5NjgxMS01NTI5LTRDQUQtQUEzNi1FRkI2M0VDQThDOEE7SURGQT01MDM2NDdEMy01QTlDLTRFMjItODA5NS1FRkUyM0Y5Mzg5QzM=",
'cookie': "aliyungf_tc=AQAAAH4w9XP89AUAWgqDdYQRcynwI6xp; q_c1=97515fa2381f48a7b6ef8d46de8bc7be|1505561812000|1505561812000; z_c0=gt2.0AAAAAAX1feoKkLNkH4BwAAAAAAtNVQJgAgBgPzk1-59vTbuXI60W0XlSl-wNBg==; _xsrf=a31505e0-0f69-440b-b357-30fffc9838bd; cap_id=\"MzQ2NGE0NThiMmVjNDU3MmE5MzVhYzE0OGYyMTYzYTI=|1505561809|0c36cb283969855952e9e895b77211fd292c0df7\"; l_cap_id=\"ODE3YzMwODM5ZTE0NDdjN2IxY2UyZDdiYjQ1MGYzNDg=|1505561809|6f707b1bf6c0a0ebb6391d0c421ac06e49ceca3b\"; r_cap_id=\"ODZlYTZmNGZhYzU3NDgwOGE4YzUyOWI5OTBmMzA3NjQ=|1505561809|05c714d01ee35a0a6c96b0c330d4f10434fec8bc\"",
'cache-control': "no-cache",
}


def get(url):
print('get url ---{}'.format(url))
z = requests.get(url, headers=headers, verify=False)

return z.json()


def run(url):
jsdata = get(url)
info = []
while True:

for i in jsdata['data']:
if i['action_type'] == 'repin':
info.append({
'url': i['member']['url'],
'name': i['member']['name'],
})
# print(i['member']['url'], print(i['member']['name']))
if not jsdata['paging']['is_end']:
jsdata = get(jsdata['paging']['next'])
else:
return info
break


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='知乎想法抽奖')
parser.add_argument('--url', type=str, default=None)
args = parser.parse_args()
baseurl = 'https://api.zhihu.com/pins/{}/actions?limit=20&offset=0'
url = args.url
pin = url.split('/')[-1]
# 修复 当网址分享到qq的时候 有后缀的问题,如https://www.zhihu.com/pin/900498393586094080?utm_source=qq
pin_id = re.search('\d+',pin)[0]
url = baseurl.format(pin_id)
info = run(url)
print(len(info))
print('********本次抽奖获得者为********')
print(choice(info))
print('********本次抽奖获得者为********')

推荐文章