-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkbaStatusCheck.py
More file actions
134 lines (110 loc) · 3.87 KB
/
Copy pathkbaStatusCheck.py
File metadata and controls
134 lines (110 loc) · 3.87 KB
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
# coding: utf-8
# Moving functions to a different file and optimizing the same
from multiprocessing import Process,Array,Value
import multiprocessing
import os
import math
import time
import requests
import bs4 as bs
import pandas as pd
import datetime
print(datetime.date.today(),'- Web scrapper','Started')
chunkSize = 8
inFile = 'KBA_list1.xlsx'
outFile = datetime.datetime.today().strftime('%d_%b_%Y')
url_pre = 'https://apps.support.sap.com/sap/support/knowledge/public/en/'
headers = {
"User-Agent": "python-requests/2.21.0",
"X-Requested-With": "XMLHttpRequest"
}
def find(kbas):
for rowNum in range(len(kbas)):
try:
kbaNum = kbas.iat[rowNum,0]
urFull = url_pre+str(kbaNum)
r = requests.get(urFull, headers=headers)
if (r.status_code == 200):
soup = bs.BeautifulSoup(r.content, "html.parser")
subText = str(soup.find('title').text.replace(u'\xa0', u''))
#For Human Readable Excel File
kbas.at[rowNum,'KBA'] = subText
#For Markdown
hyperlinkText = "["+subText+"]"
kbas.at[rowNum,'markdwn'] = hyperlinkText+"("+url_pre+str(kbaNum)+") " #for Markdown
time.sleep(1)
else:
pass
except:
pass
return kbas
def findAll(threadNumber,pivot,rowCount,cols):
killThread = False
global chunkSize
global inFile
while True :
pivotLocal = pivot.value
if pivotLocal >= rowCount.value:
killThread = True
else:
pivot.value = pivotLocal + chunkSize
if killThread == True:
break
try:
kba = pd.read_excel(inFile,header=0,skiprows=pivotLocal, nrows=chunkSize,names=cols)
kba['KBA'] = None
kba['markdwn'] = None
kba = find(kba)
finally:
kba.to_excel(str(pivotLocal)+".xls",index=False)
print("ProcessNumber = %d and Pivot = %d " % (threadNumber,pivotLocal))
def main():
print(datetime.date.today(),'- Web scrapper','Started')
global inFile
global outFile
start = time.time()
threadLimit = multiprocessing.cpu_count() #Number of Threads to be used
df = pd.read_excel(inFile)
print(datetime.date.today(),'- Input read completed')
cols = df.columns
rowCount = Value('i', 0)
rowCount.value = df.shape[0]
#df.shape[0]
pivot = Value('i', 0)
cols = Array('i', range(10))
cols = df.columns
#Start Processes
del df
threads = []
for i in range(threadLimit):
t = Process(target=findAll, args=(i,pivot,rowCount,cols))
threads.append(t)
# Start all threads
for x in threads:
x.start()
# Wait for all of them to finish
for x in threads:
x.join()
list_ = []
global chunkSize
rangeVal = math.ceil(rowCount.value/chunkSize)
print(rangeVal)
for index in range(rangeVal):
chunkFile = str(index * chunkSize)+".xls"
df = pd.read_excel(chunkFile,index_col=None, header=0)
list_.append(df)
os.remove(chunkFile)
combined_xls = pd.concat(list_, axis = 0, ignore_index = True)
combined_xls.dropna(inplace=True)
mrkdown = combined_xls[['markdwn']]
humanKBA = combined_xls[['KBA']]
#kbaHuman
humanKBA.to_excel( outFile+'.xlsx', index=False )
#Exporting the list to markdown for direct embedding in a GitHub page
mrkdown.to_csv( outFile+'.txt', index=False, header=False)
end = time.time()
print(end - start)
print(datetime.date.today(),'- Webscrapper Finshed')
if __name__ == '__main__':
__spec__ = None
main()