-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (105 loc) · 3.7 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
from selenium import webdriver
import json
def analyze(content,comments):
user=None
userStatus=None
author=None
likes = None
comment = ''
end = False
for line in content.split('\n'):
if not line.strip():
continue
if line=='Log in to like or comment.':
break
if end==True:
comments.update({(author+comment).replace(' ','+').replace('\n','*'):{"author":author,"comment":comment}})
author=None
comment=''
end=False
if line.endswith('wReply') or line.endswith('dReply') or line.endswith('yReply') or line.endswith('eReply') or line.endswith('mReply') or line.endswith('sReply') or line.endswith('hReply'):
end=True
continue
if author==user and author!=None and user!=None and comment and userStatus!=None :
end=True
continue
if author!=None and user!=None and comment and userStatus!=None and likes!=None:
end=True
continue
if userStatus==None and user==None and author==None and not comments and end==False and likes==None:
likes = line
continue
elif likes != None and (user==None or userStatus==None):
if user == None or userStatus==None:
if user == None:
user=line
author=line
continue
else :
userStatus=line
continue
else:
if author==None:
author=line
continue
else :
comment+=line
continue
return comments
def analyze2(content,comments):
user=None
userStatus=None
author=None
likes = None
comment = ''
end = False
driver = webdriver.Chrome(executable_path=r"C:\Users\Rahul\PaidProjects\PythonInstagram\gray-scraper\chromedriver.exe")
driver.get('https://www.instagram.com/p/B0OsWFXlPqq/')
i=0
comments = dict()
replies=dict()
try:
elem2 = driver.find_element_by_class_name('eo2As')
f0=open('test.txt','w',encoding='utf-8')
f0.write(str(elem2.text))
f0.close
fo=open('test.txt','r',encoding='utf-8')
st=fo.read()
fo.close()
comments=analyze(st,comments)
while driver.find_elements_by_class_name('Igw0E.IwRSH.YBx95._4EzTm.MGdpg.NUiEW')!=None:
#print('HERE')
i+=1
elem1 = driver.find_element_by_class_name('Igw0E.IwRSH.YBx95._4EzTm.MGdpg.NUiEW')
elem2 = driver.find_element_by_class_name('eo2As')
try :
elem3=driver.find_element_by_class_name('Igw0E.IwRSH.eGOV_.ybXk5._4EzTm')
elem3.click()
elem3=driver.find_element_by_class_name('TCSYW')
f0=open('test.txt','w',encoding='utf-8')
f0.write(str(elem3.text))
f0.close
fo=open('test.txt','r',encoding='utf-8')
st=fo.read()
fo.close()
replies=analyze(st,replies)
print('Analyzed Replies',end='\n')
except Exception as err:
print('No Replies',end='\n')
f0=open('test.txt','w',encoding='utf-8')
f0.write(str(elem2.text))
f0.close
fo=open('test.txt','r',encoding='utf-8')
st=fo.read()
fo.close()
comments=analyze(st,comments)
print('click ',i,' Analyzed ',len(comments),' Comments in total recieved',end='\n')
elem1.click()
#src.append(driver.page_source)
except Exception as err:
print(err)
finally :
comments.update({"replies":replies})
with open('data.json', 'w',encoding='utf-8') as fp:
json.dump(comments, fp)
print('Got ',len(comments),' comments and ',len(replies),' Replies')