1+ # coding:utf-8
2+ import requests
3+ from bs4 import BeautifulSoup
4+ import time
5+
6+ url = 'https://github.com/'
7+ default_url = 'https://github.com'
8+ headers = {
9+ 'User-Agent' : # ur user-Agent here
10+ }
11+
12+ result_lists = []
13+
14+ def counts2pages (Tag ):
15+ str = Tag .get_text ().replace (' ' ,'' ).strip ('\n ' ).encode ('utf-8' )
16+ leng = len (str )- 1
17+ if str [- 1 ] == 'k' :
18+ pages = int (float (str [:leng ]) * 1000 )/ 51 + 1
19+ else :
20+ pages = int (str )/ 51 + 1
21+ return pages
22+
23+
24+ def get_follow_counts_pages (url ,person ,boo ):
25+ followXXX = 'following' if boo else 'followers'
26+ true_url = url + person + '?tab=' + followXXX
27+ data = requests .get (true_url , headers = headers )
28+ soup = BeautifulSoup (data .text , 'lxml' )
29+ counts = soup .select ('div.user-profile-nav > nav > a.underline-nav-item > span' )
30+ # 如果boo 是真,说明是following ,counts[3]
31+ if boo :
32+ return counts2pages (counts [3 ])
33+ else :
34+ return counts2pages (counts [2 ])
35+
36+ def get_follow_lists (url ,person ,boo ,pages = 1 ):
37+ # 组url
38+ followXXX = 'following' if boo else 'followers'
39+ true_url = url + person + '?page=' + str (pages ) + '&tab=' + followXXX
40+ data = requests .get (true_url , headers = headers )
41+ soup = BeautifulSoup (data .text , 'lxml' )
42+ lists = soup .select ('div.d-table > div.d-table-cell > a.d-inline-block' )
43+ if lists == []:
44+ print 'yaoxiugaiisgoon'
45+ for i in lists :
46+ result_lists .append (default_url + i .get ('href' ))
47+ print default_url + i .get ('href' )
48+ print len (result_lists )
49+ return result_lists
50+
51+ for i in range (1 ,10 ):
52+ get_follow_lists (url ,'ruanyf' ,True ,i )
0 commit comments