[infobox title="前言"]

之前看到 twitter 上有人说,想知道关于通过多少个关注者,就可以特定出来某人是谁,或者把可能的人数缩小的个位数。

回想起咱自己天天咕咕咕的时光,6 月俨然又要结束,自己一篇 blog 都没有水。。。突然觉得摸鱼好像有点太过忘我了。。。于是便水了这篇文章。。。。

大概连教程都算不上就是了。。。底下贴的代码。。。用过的人应该都看得出来。。。是从 Jupyter Notebook 直接拷贝过来的quq。。。。。连网页格式都懒得改的那种。。。(以后有机会再想办法优化?)

[/infobox]

[infobox title="需要的东西"]

一台联网的电脑 + twitter 的 AccessKey 和 AccessSecret 。。

Jupyter Notebook 是一个很好的一边写 python 一边执行的环境 (数据分析常用的工具?)。。。总之咱最终用的它。。。

[/infobox]
[infobox title="代码就在这个下面quq"]

In [ ]:
import pandas as pd
from time import sleep
from requests import get, post
from hashlib import sha256 as ss
from base64 import b64encode as b64e

def sha256sum(value):
## 虽然没什么必要,不过可以通过修改“salt”为你想要的任何字符串来自定义盐哦!
s=str(value)+"salt"
return ss(bytes(s,encoding = "utf8")).hexdigest()

In [ ]:
AK=b64e(bytes(input("请按照Access_key:Access_secret"),encoding = "utf8")).decode('ascii')

token=post('https://api.twitter.com/oauth2/token',
headers = {
"Authorization": "Basic "+AK,
"Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"},
data = {
"grant_type": "client_credentials"}).json()['access_token']
print("token为"+token)

In [ ]:
BaseUrl='https://api.twitter.com/1.1/followers/ids.json'
UN=input('请输入不包含@的用户名')
NC=-1
UserList=[]
while NC != 0 :
    res=get(BaseUrl, 
                 headers = {
                    "Authorization": "Bearer "+token},
                 params = {
                     "count": 5000,
                     "screen_name": UN,
                     "cursor": NC}).json()
    UserList.extend(res['ids'])
    NC=res['next_cursor']
#    sleep(61)
In [ ]:
input('回车键继续')
BaseUrl='https://api.twitter.com/1.1/friends/ids.json'
IDL=pd.DataFrame()
CNT=1

###
# 下面这行表示只抓取列表前10个用户的关注,如果需要所有人,请注释下面那行
# 并取消更下面一行的注释(将可能会浪费非常多的时间)
# 请谨慎取消注释
###
for UID in UserList[:9] :
#for UID in UserList :
NC=-1
FL=[]
while NC != 0:
CNT+=1
try:
res=get(BaseUrl,
headers = {
"Authorization": "Bearer "+token},
params = {
"count": 5000,
"user_id": UID,
"cursor": NC}).json()
if(CNT%15 == 0):
print('目前进度{}/{},为防止过热,15分钟后再见quq'.format(UserList.index(UID) ,len(UserList)))
sleep(15*60)
FL.extend(res['ids'])
NC=res['next_cursor']
except:
NC=0
print("UID:{}的用户获取失败".format(UID))
continue
FFL=[]
for iu in FL:
FFL.append(sha256sum(iu))
IDL=IDL=pd.concat([IDL,pd.DataFrame({str(sha256sum(UID)):FFL})], axis=1)

In [ ]:
###
# 将会输出一个以输入的用户名为文件名的经历了sha256处理的csv文件
# 方便日后操作的同时尽量降低通过保存的文件特定化某人的风险。。。(大概)
###
IDL.to_csv("{}.csv".format(UN))
[/infobox]