-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
176 lines (149 loc) · 4.84 KB
/
main.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Import libraries
import requests
import urllib.request
import time
from bs4 import BeautifulSoup
import fileinput
import json
import time
import concurrent.futures
biglist=[]
url =[]
url2 =[]
track1=[
'Getting Started: Create and Manage Cloud Resources',
'Perform Foundational Infrastructure Tasks in Google Cloud',
'Set up and Configure a Cloud Environment in Google Cloud',
'Deploy and Manage Cloud Environments with Google Cloud',
'Build and Secure Networks in Google Cloud',
'Deploy to Kubernetes in Google Cloud'
]
track2 = [
'Getting Started: Create and Manage Cloud Resources',
'Perform Foundational Data, ML, and AI Tasks in Google Cloud',
'Insights from Data with BigQuery',
'Engineer Data in Google Cloud',
'Integrate with Machine Learning APIs',
'Explore Machine Learning Models with Explainable AI'
]
#get the url in list
def data_scraping (url):
#print("in data scraping")
with fileinput.input(files=('userurl.txt')) as f:
for line in f:
url.append(line.replace("\n", ""))
for ele in url:
if ele.strip():
url2.append(ele)
start_thread(url2)
# Connect to the URL
def data_gathering(link):
#t3 = time.time()
#print("data gathering")
tempdic = {}
response = requests.get(link)
soup = BeautifulSoup(response.text, "html.parser")
track1completed = []
track2completed = []
profile = soup.findAll('div', attrs = {'class':'public-profile__hero'})[0]
dp = profile.img['src']
name = profile.h1.text
tempdic['name'] = name.strip()
tempdic['dp'] = dp
#tempdic['qlabid'] = link
quests = soup.findAll('ql-badge')
for quest in quests:
allquest = json.loads(quest.get('badge'))['title']
#print(allquest)
if allquest in track1:
track1completed.append(allquest)
if allquest in track2:
track2completed.append(allquest)
tempdic['track1'] = track1completed
tempdic['track2'] = track2completed
tempdic['qcomplete_no'] = len(track1completed) + len(track2completed)
#print(tempdic['qcomplete_no'])
if tempdic['qcomplete_no']!=0:
print(len(biglist)," ",tempdic['name']," ",tempdic['qcomplete_no']," ",tempdic['track1']," ",tempdic['track2'])
biglist.append(tempdic)
print("data saved")
else:
print("no badges")
#t4 = time.time()
#print(f"{t4-t3} seconds to download this profile.")
#print("data saved")
"""
badge = soup.find_all('ql-badge').attrs
print(badge)
qname = badge['badge'].split(',')
qname2 = qname[1].split(":")
print(qname2)
"""
"""
list = []
for row in quests[0].findAll('ql-badge'):
list.append(str(row))
print(list)
for element in list :
print(element)
point = element.split('=')
print(point[1])
"""
"""
for row in quests[0].findAll('div', attrs = {'class':'public-profile__badge'}):
divs = row.findChildren("div" , recursive=False)
if divs[1].text.strip() in track1:
track1completed.append(divs[1].text.strip())
if divs[1].text.strip() in track2:
track2completed.append(divs[1].text.strip())
tempdic['track1'] = track1completed
tempdic['track2'] = track2completed
tempdic['qcomplete_no'] = len(track1completed) + len(track2completed)
print(temdic['qcomplete_no'])
print(len(biglist)," ",tempdic['name']," ",tempdic['qcomplete_no']," ",tempdic['track1']," ",tempdic['track2'])
#if tempdic['qcomplete_no']!=0:
biglist.append(tempdic)
print("data saved")
#else:
# print("data not saved")
t4 = time.time()
print(f"{t4-t3} seconds to download this profile.")
"""
def data_saving (biglist):
print("in data saving")
#print(biglist)
#print("The original dictionary : " + str(biglist))
#print("\n")
res = sorted(biglist, key = lambda x: x['qcomplete_no'], reverse=True)
#print("The sorted dictionary by marks is : " + str(res))
with open('finallist.txt', 'w') as f:
print(biglist, file=f)
with open('sortedfinallist.txt', 'w') as f:
print(res, file=f)
with open("my.json","w") as f:
json.dump(res,f)
f.close()
def start_thread(url2):
"""
print("start thread")
id = 0
for link in url2:
print("start thread loop")
data_gathering(link)
id+=1
print("start thread loop ended")
data_saving(biglist)
"""
threads = 10
#print("in start thread")
with concurrent.futures.ThreadPoolExecutor(max_workers=threads) as executor:
executor.map(data_gathering, url2)
data_saving (biglist)
def main(url):
#print("in main")
data_scraping (url)
t0 = time.time()
main(url)
t1 = time.time()
print(f"{t1-t0} seconds to download {len(url2)} profile.")
print("number of people started",len(biglist))