-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpackage.py
163 lines (118 loc) · 4.44 KB
/
package.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
__Author__ = "Soumil Nitn Shah"
__Version__ = '0.0.1'
__Email__ = "[email protected]"
__Website__ ="https://www.soumilshah.herokuapp.com"
try:
import requests
from bs4 import BeautifulSoup
import pandas as pd
except Exception as e:
print("Some Modules are Missing {}".format(e))
class Request(object):
def __init__(self, url = 'https://github.com/soumilshah1995?tab=repositories'):
self.url = url
self.headers = {
'Accept-Encoding': 'gzip, deflate, sdch',
'Accept-Language': 'en-US,en;q=0.8',
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
'Referer': 'http://www.wikipedia.org/',
'Connection': 'keep-alive',
}
def get(self):
"""
:return: Response
"""
try:
r = requests.get(url=self.url, headers = self.headers)
return r
except Exception as e:
print("Failed to make response ")
class HashMap(object):
def __init__(self):
self.data = {
'RepoName':[],
'url':[],
'No':[]
}
class GithubHunter(object):
def __init__(self, username= '', pages=5):
self.username = username
self.url = "https://github.com/"+self.username+"?tab=repositories"
self.resp = Request(self.url)
self.soup = self.createSoup()
self.datastructure = HashMap()
self.pages = pages
def createSoup(self):
soup = BeautifulSoup(self.resp.get().text, 'html.parser')
return soup
def getToken(self, soup):
"""
Gets the Next Page Token
:return: Url
"""
token = soup.findAll('div', class_='BtnGroup')
for x in token:
for y in x.findAll('a', class_="btn btn-outline BtnGroup-item"):
return y["href"]
def getData(self, soup):
li = soup.findAll('div', class_='d-inline-block mb-1')
base_url = "https://github.com/"
# Page 1 Scrapper
for _, i in enumerate(li):
for a in i.findAll('a'):
newUrl = base_url + a["href"]
self.datastructure.data["No"].append(_)
self.datastructure.data["RepoName"].append(i.text.strip())
self.datastructure.data["url"].append(newUrl)
def run(self):
# Get the Token for Next page
token = self.getToken(self.soup)
# scrape Page 1
self.getData(self.soup)
tem = ''
for i in range(1,self.pages):
try:
if i==1:
r = requests.get(url=token)
data = r.text
soup = BeautifulSoup(data, 'html.parser')
token = self.getToken(soup)
self.getData(soup)
tem = token
else:
r = requests.get(url=tem)
data = r.text
soup = BeautifulSoup(data, 'html.parser')
token = self.getToken(soup)
self.getData(soup)
tem = token
except Exception as e:
pass
data = list(zip(self.datastructure.data["RepoName"],
self.datastructure.data["url"]))
df = pd.DataFrame(data=data, columns=["Repo Name", "url"])
print("Hunt Complete ....... ")
return df
def createCSV(self):
data = list(zip(self.datastructure.data["RepoName"],
self.datastructure.data["url"]))
df = pd.DataFrame(data=data, columns=["Repo Name", "url"])
print(df)
df.to_csv("Repo.csv")
def saveExcel(self):
data = list(zip(self.datastructure.data["RepoName"],
self.datastructure.data["url"]))
df = pd.DataFrame(data=data, columns=["Repo Name", "url"])
print(df)
df.to_excel("Repo.xls")
def saveJson(self):
data = list(zip(self.datastructure.data["RepoName"],
self.datastructure.data["url"]))
df = pd.DataFrame(data=data, columns=["Repo Name", "url"])
print(df)
df.to_json("Repo.json")
# if __name__ == "__main__":
# hunter = GithubHunter(username = "dathu", pages=2)
# df = hunter.run()
# print(df)