-
Notifications
You must be signed in to change notification settings - Fork 7
/
userid.py
57 lines (48 loc) · 1.45 KB
/
userid.py
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
# -*- coding: utf-8 -*-
"""
Created on Sat Dec 8 16:42:42 2018
@author: Administrator
"""
import sys
import requests
import json
import time
import random
import codecs
import importlib
import traceback
importlib.reload(sys)
infofile = codecs.open("Weibo_List_p.txt", 'a', 'utf-8')
def crawlDetailPage(url,page):
global ID_get
#global num
global infofile
#读取微博网页的JSON信息
req = requests.get(url)
jsondata = req.text
data = json.loads(jsondata)
#获取每一条页的数据
content = data['data'].get('cards')
#print(content)
#循环输出每一页的关注者各项信息
try:
for i in content:
followingId = i['user']['id']
#print(followingId)
ID_get.append(followingId)
#num=num+1
infofile.write(str(followingId)+ '\r\n')
except Exception as e:
print ("Error: ",e)
traceback.print_exc()
user_oid=1005052766134004 #另一个的是: 1005055984336074
ID_get = [];
for i in range(1, 501):
print("正在获取第{}页的粉丝列表:".format(i))
# 微博用户关注列表JSON链接
url = "https://m.weibo.cn/api/container/getSecond?containerid={user_oid}_-_FANS&page={page}".format(user_oid=user_oid, page=i) # page=" + #FOLOWERS关注,FANS粉丝
crawlDetailPage(url, i)
# 设置休眠时间
t = random.randint(2, 5)
print("休眠时间为:{}s".format(t))
time.sleep(t)