-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
103 lines (59 loc) · 2.53 KB
/
scraper.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
import asyncio
import threading
from database import db
from pinterest import SiteScraper
from redis_conn import redis_conn
import time
class Scraper:
def __init__(self, drivernum):
self.drivers = []
self.r = redis_conn()
self.imgs = set()
self.used_words = set()
self.drivernum = drivernum
def init_scraper(self):
for i in range(0, self.drivernum):
dr = SiteScraper()
dr.run_scrape()
for word in dr.return_search_words():
self.r.write_word(word)
for img in dr.return_sources():
self.r.write_image(img)
self.drivers.append(dr)
print("no. of drivers initiated: " + str(len(self.drivers)))
def run_searches(self, driver, limit):
# pops search word from the redis table,
# loads up that page, gets images and search words from it
# deposits both to appropriate redis tables
# repeats until runs out of search words or reaches a set limit
search_word = self.r.get_word()
while search_word != 0 and search_word != None and len(self.used_words) < limit:
# add to a list of words that have been searched for to avoid duplicates
self.used_words.add(search_word)
# load new search term
driver.load_search(search_word)
# get results from new search term
driver.run_scrape()
words = driver.return_search_words()
for word in words:
if word not in self.used_words:
self.r.write_word(word)
for img in driver.return_sources():
self.r.write_image(img)
# fetch another search term
search_word = self.r.get_word()
print("used: " + str(len(self.used_words)))
driver.close()
return 1
def run_threads(self, limit=100):
self.init_scraper()
for driver in self.drivers:
threading.Thread(target=self.run_searches, args=[driver, limit]).start()
#async def Run(self):
# loop = asyncio.get_event_loop()
#loop.run_until_complete(asyncio.ensure_future(self.InitScraper()))
# self.InitScraper(1)
# tasks = []
# for driver in self.drivers:
# tasks.append(asyncio.ensure_future(self.RunSearches(driver)))
# loop.run_until_complete(asyncio.gather(*tasks))