-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstagram_scrape.py
More file actions
166 lines (145 loc) · 9.11 KB
/
instagram_scrape.py
File metadata and controls
166 lines (145 loc) · 9.11 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
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
import os
import time
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
XPATHS = {
# Instagram Login XPaths
'login_with_instagram': '/html/body/div/div[1]/div/div[2]/div[1]/div/div[1]/div[2]/div/div/div/div[6]/div/button',
'username_input': '//*[@id="loginForm"]/div/div[1]/div/label/input',
'password_input': '//*[@id="loginForm"]/div/div[2]/div/label/input',
'login_button': '//*[@id="loginForm"]/div/div[3]',
# Instagram Overview Data XPaths
'insights_page': '/html/body/div[1]/div[1]/div/div/div/div/div[2]/div/div/div[1]/div/div/div[2]/div/div[1]/nav/ul/div/div[7]/div/div/li/div/div/a',
'close_popup': '//*[@id="facebook"]/body/div[4]/div[1]/div[1]/div/div/div/div/div[3]/div/div/div',
'reach': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div/div/div[1]/div[2]/div/div/div/div[2]/div[1]/div[2]/div[3]/div[2]/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div[2]/div[1]',
'followers': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[3]/div/div/div[1]/div[2]/div/div/div/div[2]/div[1]/div[3]/div[3]/div[2]/div/div/div[2]/div[1]/div/div/div/div/div/div/div/div[2]/div',
# Instagram Results Data XPaths
'results_page': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[1]/div[2]/div/div/div/div/div[1]/div[2]/div[1]/div[1]/div/nav[1]/div[2]/div[2]/div[1]/a/div/div',
'profile_visits': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div[3]/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/div/div[2]/div/div/div/div/div[2]/div/div/div[1]/div/div[1]/div/div/div/span[1]',
'new_followers': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div[4]/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/div/div[2]/div/div/div/div/div[2]/div/div/div[1]/div/div[1]/div/div/div/span[1]',
# Instagram Content Data XPaths
'content_page': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[1]/div[2]/div/div/div/div/div[1]/div[2]/div[1]/div[1]/div/nav[2]/div[2]/div[1]/div[1]/a/div/div',
'post_engagement': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/div/div/div/div[2]/div/div[6]/div/div[2]/div/div[1]/div/div/div/div[2]/div[1]',
# Instagram Stories Data XPaths
'stories_page': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/div/div/div/div[1]/div[4]/div/div/span[1]',
'story_reach': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/div/div/div/div[2]/div/div[2]/div/div[2]/div/div[1]/div/div/div/div[2]/div[1]',
'story_engagement': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/div/div/div/div[2]/div/div[6]/div/div[2]/div/div[1]/div/div/div/div[2]/div[1]',
'published_stories': '//*[@id="facebook"]/body/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div[1]/div[1]/div/div/div/div/div/div[2]/div/div[1]/div/div[2]/div/div/div[2]/div/div/div/div[1]/div[2]/div/div/div/div[2]/div/div/div/div/div[2]/div/div[4]/div/div[2]/div/div[1]/div/div/div/div[2]/div[1]'
}
class InstaScraper:
def __init__(self):
self.user = '' # input the user name
self.password = '' # input the password
self.driver = self._setup_driver()
def _setup_driver(self):
options = webdriver.ChromeOptions()
options.add_argument('--disable-dev-shm-usage')
options.add_argument('lang=en')
options.add_argument('start-maximized')
options.add_argument('disable-infobars')
options.add_argument('--disable-extensions')
options.add_argument('--incognito')
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(options=options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined});")
return driver
def _wait_for_element(self, xpath, timeout=10):
return WebDriverWait(self.driver, timeout).until(EC.presence_of_element_located((By.XPATH, xpath)))
def _login(self):
try:
self.driver.get('https://business.facebook.com/') # Your initial login URL here
self._wait_for_element(XPATHS['login_with_instagram']).click()
self._wait_for_element(XPATHS['username_input']).send_keys(self.user)
self._wait_for_element(XPATHS['password_input']).send_keys(self.password)
self._wait_for_element(XPATHS['login_button']).click()
time.sleep(10)
self.driver.get('https://business.facebook.com/')
self._wait_for_element(XPATHS['login_with_instagram']).click()
except Exception as e:
print(f"Error during login: {e}")
def scrape_overview_data(self):
try:
time.sleep(5)
self._wait_for_element(XPATHS['insights_page']).click()
time.sleep(5)
self._wait_for_element(XPATHS['close_popup']).click()
time.sleep(5)
reach = self._wait_for_element(XPATHS['reach']).text
followers = self._wait_for_element(XPATHS['followers']).text
return {
'username': self.user,
'reach': reach,
'followers': followers
}
except Exception as e:
print(f"Error fetching overview data: {e}")
return {}
def scrape_results_data(self):
try:
time.sleep(5)
self._wait_for_element(XPATHS['results_page']).click()
time.sleep(5)
profile_visits = self._wait_for_element(XPATHS['profile_visits']).text
new_followers = self._wait_for_element(XPATHS['new_followers']).text
return {
'profile_visits': profile_visits,
'new_followers': new_followers
}
except Exception as e:
print(f"Error fetching results data: {e}")
return {}
def scrape_content_data(self):
try:
time.sleep(5)
self._wait_for_element(XPATHS['content_page']).click()
time.sleep(5)
post_engagement = self._wait_for_element(XPATHS['post_engagement']).text
return {
'post_engagement': post_engagement
}
except Exception as e:
print(f"Error fetching content data: {e}")
return {}
def scrape_stories_data(self):
try:
time.sleep(5)
self._wait_for_element(XPATHS['stories_page']).click()
time.sleep(5)
story_reach = self._wait_for_element(XPATHS['story_reach']).text
story_engagement = self._wait_for_element(XPATHS['story_engagement']).text
published_stories = self._wait_for_element(XPATHS['published_stories']).text
return {
'story_reach': story_reach,
'story_engagement': story_engagement,
'published_stories': published_stories
}
except Exception as e:
print(f"Error fetching stories data: {e}")
return {}
def scrape_data(self):
self._login()
overview_data = self.scrape_overview_data()
results_data = self.scrape_results_data()
content_data = self.scrape_content_data()
stories_data = self.scrape_stories_data()
self.driver.quit()
return {**overview_data, **results_data, **content_data, **stories_data}
class DataSaver:
@staticmethod
def save(data):
df = pd.DataFrame(data, index=[0])
df.to_csv('insta_data.csv', index=False)
'''
if not os.path.isfile('insta_data.csv'):
df.to_csv('insta_data.csv', index=False)
else:
existing_df = pd.read_csv('insta_data.csv')
updated_df = pd.concat([existing_df, df], ignore_index=True)
updated_df.to_csv('insta_data.csv', index=False)
'''
scraper = InstaScraper()
data = scraper.scrape_data()
DataSaver.save(data)