-
Notifications
You must be signed in to change notification settings - Fork 0
/
insta_crawling.py
192 lines (156 loc) · 6 KB
/
insta_crawling.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
import os
import re
import time
import logging
import datetime
import argparse
import numpy as np
import pandas as pd
import requests
from bs4 import BeautifulSoup
from selenium import webdriver
class InstaKeywordCrawling():
def __init__(self, email, password):
self.email = email
self.password = password
if not os.path.exists('./data'):
os.mkdir('./data')
# options = webdriver.ChromeOptions()
# options.add_argument("headless")
# self.driver = webdriver.Chrome('./etc/chromedriver.exe', options=options)
self.driver = webdriver.Chrome('./etc/chromedriver.exe')
def login(self):
self.driver.get("https://www.instagram.com")
time.sleep(3)
input_id = self.driver.find_elements_by_css_selector('input._2hvTZ.pexuQ.zyHYP')[0]
input_id.clear()
input_id.send_keys(self.email)
input_pw = self.driver.find_elements_by_css_selector('input._2hvTZ.pexuQ.zyHYP')[1]
input_pw.clear()
input_pw.send_keys(self.password)
input_pw.submit()
time.sleep(3)
def get_insta_url(self, keyword):
return f'https://www.instagram.com/explore/tags/{str(keyword)}'
def search_first(self):
first = self.driver.find_element_by_css_selector("div._9AhH0")
first.click()
time.sleep(5)
def get_content(self):
html = self.driver.page_source
soup = BeautifulSoup(html, 'lxml')
try:
contents = soup.select('div.C4VMK > span')
content = contents[0].text
rewiew = list(map(lambda x:x.text, contents[1:]))
except:
content = ' '
rewiew = []
try:
tags = re.findall(r'#[^\s#,\\]+', content)
except:
tags = ''
try:
date = soup.select('time._1o9PC.Nzb55')[0]['datetime'][:10]
except:
date = ''
try:
like = int(soup.select('div.Nm9Fw > a.zV_Nj > span')[0].text)
except:
like = 0
try:
place = soup.select('div.M30cS')[0].text
except:
place = ''
return [content, rewiew, date, like, place, tags]
def move_next(self):
right = self.driver.find_element_by_css_selector("body > div._2dDPU.CkGkG > div.EfHg9 > div > div > a._65Bje.coreSpriteRightPaginationArrow")
right.click()
time.sleep(5)
def crawling(self, keyword, num_of_sample=300):
self.keyword = keyword
self.num_of_sample = num_of_sample
url = self.get_insta_url(keyword)
self.driver.get(url)
time.sleep(5)
for i in range(num_of_sample):
if i == 0:
self.search_first()
data = self.get_content()
self.result = [data]
else:
try:
self.move_next()
data = self.get_content()
self.result.append(data)
except:
time.sleep(2)
self.move_next()
def save_result(self):
now = datetime.datetime.now()
now = str(now.strftime('%Y%m%d_%H'))
results_df = pd.DataFrame(self.result)
results_df.columns = ['content', 'rewiew', 'date', 'like', 'place', 'tags']
results_df.to_csv(f'./data/{self.keyword}_{now}.csv', index=False)
def new_crawling(self, keyword, num_of_scroll=100):
self.keyword = keyword
print(keyword)
url = self.get_insta_url(keyword)
self.driver.get(url)
time.sleep(5)
paths = self.get_paths(num_of_scroll)
print('crawling success')
self.result = []
self.num_of_sample = len(paths)
print('num_of_sample : ', self.num_of_sample)
for path in paths:
self.driver.get(path)
time.sleep(5)
try:
data = self.get_content()
self.result.append(data)
except:
time.sleep(2)
print('scraping success')
def get_paths(self, num_of_scroll):
SCROLL_PAUSE_SEC = 3
html = self.driver.page_source
soup = BeautifulSoup(html, 'lxml')
paths = self.update_paths([], soup)
last_height = self.driver.execute_script("return document.body.scrollHeight")
for count in range(num_of_scroll):
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(SCROLL_PAUSE_SEC)
html = self.driver.page_source
soup = BeautifulSoup(html, 'lxml')
paths = self.update_paths(paths, soup)
now_height = self.driver.execute_script("return document.body.scrollHeight")
new_height = now_height
while now_height == last_height:
new_height -= 100
self.driver.execute_script(f"window.scrollTo(0, {new_height});")
time.sleep(1)
self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
now_height = self.driver.execute_script("return document.body.scrollHeight")
last_height = new_height
return list(set(paths))
def update_paths(self, paths, soup):
for row in soup.select('div.Nnq7C.weEfm'):
for column in row.select('div.v1Nh3.kIKUG._bz0w > a'):
path = 'https://www.instagram.com'+str(column).split(' ')[1][6:-1]
paths.append(path)
return paths
def main(keyword_list):
email = '*********'
password = '*********'
insta = InstaKeywordCrawling(email, password)
insta.login()
for keyword in list(keyword_list.split(',')):
insta.new_crawling(keyword)
insta.save_result()
insta.driver.quit()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--keyword', required=True, type=str, help='tag keyword')
args = parser.parse_args()
main(args.keyword)