Skip to content

Commit 6cf596c

Browse files
committed
githubspider
1 parent bfb8882 commit 6cf596c

3 files changed

Lines changed: 77 additions & 34 deletions

File tree

‎README.md‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,26 @@
11
# pythonSpider
2-
some python spiders with BeautifulSoup
2+
some python spiders with BeautifulSoup
3+
4+
## githubSpider
5+
6+
###githubFollowXXList 模块:
7+
8+
```
9+
get_follow_counts_pages(url,person,boo):
10+
```
11+
12+
**url**就是github的网址:'https://github.com/';**person**是想要获取用户的username;
13+
14+
**boo**是一个布尔值,若是真,则获取following,若是假,则获取followers
15+
16+
可以获取当前人物的获取following或者followers的页数
17+
18+
```
19+
get_follow_lists(url,person,boo,pages):
20+
```
21+
22+
**url**, **person** ,**boo**和上一个方法一样,**pages**是指想获取第几页的following或者followers的列表
23+
24+
方法返回当前page的following或者followers的列表,结合```
25+
get_follow_counts_pages
26+
```可以获取所以的following或者followers的列表

‎githubspider/4followingList.py‎

Lines changed: 0 additions & 33 deletions
This file was deleted.
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
 (0)