-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstascraper.py
More file actions
40 lines (38 loc) · 1.43 KB
/
instascraper.py
File metadata and controls
40 lines (38 loc) · 1.43 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
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import csv
userlist=['triangl0321','_iamhritik']#Add all your usernames here to get their data very easily.
options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')
options.add_argument('--headless')
driver = webdriver.Chrome(executable_path="./chromedriver", chrome_options=options)
for username in userlist:
postdata=[]
profiledata=[]
driver.get('https://www.instagram.com/'+username+'/?hl=en')
Pagelength=driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
source=driver.page_source
data=bs(source,'html.parser')
body=data.find('body')
profiles=driver.find_elements_by_class_name('_6q-tv')
posts=driver.find_elements_by_class_name('FFVAD')
Title1=['PROFILE PICTURE URL']
Title2=['POSTS URL']
newline = [' ']
print("Found data for this user:"+username)
filename=username+".csv"
with open(filename,'w')as csvfile:
csvwriter=csv.writer(csvfile,delimiter=',')
csvwriter.writerow(Title1)
for profile in profiles:
sourceprofile=(profile.get_attribute('src'))
profiledata.append(sourceprofile)
csvwriter.writerow(profiledata)
csvwriter.writerow(newline)
csvwriter.writerow(Title2)
for post in posts:
sourceposts=(post.get_attribute('src'))
postdata.append(sourceposts)
csvwriter.writerow(postdata)
driver.close()
print("Successfully stored all the data in the csv file format.\nThanks for using it.")