-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstaScrapper.py
158 lines (123 loc) · 5.19 KB
/
instaScrapper.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
from selenium import webdriver
from bs4 import BeautifulSoup as bs
import time
import re
from urllib.request import urlopen
import json
from pandas.io.json import json_normalize
import pandas as pd, numpy as np
import glob
# dirrectory where to save files with info
directory_path = '_results_files'
# path to chrome driver
browser = webdriver.Chrome('/Users/vasily/Documents/00_PYTHON/InstaScrapper/chromedriver')
links = []
# print(len(links))
# how many pages scrap
n_pages = 200
# hashtag / user
mode = 'hashtag'
# list of hashtags / usernames
name_list = ['tattoodesign']
#name_list = ['liwenliang']
# , 'selfiestick', 'selfiequeen'
# save info each ... (in case of bad connection orsome error during scrapping better do not wait till last /
# also easier to stop script if you want without loosing all data)
save_after = 50
def scrollPage(n_pages = 1):
print("PAGE : 0")
Pagelength = browser.execute_script("window.scrollTo(0, document.body.scrollHeight);")
source = browser.page_source
data=bs(source, 'html.parser')
body = data.find('body')
script = body.find('span')
for link in body.findAll('a'):
#print(link)
if re.match("/p", link.get('href')):
links.append('https://www.instagram.com'+link.get('href'))
time.sleep(5)
if n_pages > 1:
for i in range(n_pages-1):
print("PAGE : {}".format(i+1))
Pagelength = browser.execute_script("window.scrollTo(document.body.scrollHeight/2, document.body.scrollHeight);")
source = browser.page_source
data=bs(source, 'html.parser')
body = data.find('body')
script = body.find('span')
for link in body.findAll('a'):
if re.match("/p", link.get('href')):
links.append('https://www.instagram.com'+link.get('href'))
time.sleep(5)
def getInfo(links, name, save_after):
# read the file with images we already parsed (use list_dir.py to create ths file first)
shortcodes = pd.read_csv('shortcodes.csv')
#do not replace already existing files
cur_file = 0
files = glob.glob("{}/{}_*".format(directory_path,name), recursive=True)
print("FILES with {} - {}".format(name,len(files)))
cur_file = len(files)
links = list(set(links))
if len(links) > 0:
result=pd.DataFrame()
q = 0
for i in range(len(links)):
# check if we alreay parsed this image
short = [links[i].split('/')[4]]
m = shortcodes.isin(short).any()
cols = m.index[m].tolist()
# if not
if len(cols) == 0:
try:
print("{} {}".format(i, links[i]))
page = urlopen(links[i]).read()
data=bs(page, 'html.parser')
body = data.find('body')
script = body.find('script')
raw = script.text.strip().replace('window._sharedData =', '').replace(';', '')
json_data=json.loads(raw)
posts =json_data['entry_data']['PostPage'][0]['graphql']
posts= json.dumps(posts)
posts = json.loads(posts)
x = pd.DataFrame.from_dict(json_normalize(posts), orient='columns')
x.columns = x.columns.str.replace("shortcode_media.", "")
result=result.append(x)
q += 1
except:
print(' CANT PARSE IMAGE')
np.nan
if q > save_after:
result = result.drop_duplicates(subset = 'shortcode')
result.to_csv('{}/{}_{}.csv'.format(directory_path, name, cur_file))
print('-' * 30)
print("FILE SAVED : {}_{}.csv".format(name, cur_file))
cur_file += 1
result=pd.DataFrame()
q = 0
else:
print('{} {} Already parsed'.format(i, links[i]))
result = result.drop_duplicates(subset = 'shortcode')
result.to_csv('{}/{}_{}.csv'.format(directory_path, name, cur_file))
print('-' * 10)
print("FILE SAVED : {}_{}.csv".format(name, cur_file))
del links[:]
def instaScrapper( name_list, n_pages, mode = 'hashtag', save_after=100):
if mode == 'user':
for name in name_list:
print(' ')
print('-' * 30)
print("PARSING NAME : {}".format(name))
username = name
browser.get('https://www.instagram.com/'+username+'/?hl=en')
scrollPage(n_pages)
print(len(links))
getInfo(links, name, save_after)
else:
for name in name_list:
print(' ')
print('-' * 30)
print("PARSING NAME : {}".format(name))
hashtag = name
browser.get('https://www.instagram.com/explore/tags/'+hashtag)
scrollPage(n_pages)
getInfo(links, name, save_after)
instaScrapper( name_list, n_pages, mode, save_after)