-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
64 lines (55 loc) · 1.65 KB
/
main.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
# import libraries
import asyncfunctions as af
import os, sys, json, time
import numpy as np
import requests
# import logic libraries
import logic
from printer import print_green, print_red, print_yellow
# get the start input
try: st = int(sys.argv[1])
except: st = 0
# clear the terminal
os.system("cls")
beginning = time.time()
# prepare the destionation folder
dest_folder = "./data/"
speed = [] # timespeed variable
# read all the product urls
urls = json.load(open("./urls/product_urls.json"))
urls = list(dict.fromkeys(urls))
# iterate the scraping process in batches
index = list(range(st,len(urls)))
# filter the scraped urls
root = "./data/"
for file in os.listdir(root):
if file.endswith("json"):
i = int(file.replace(".json",""))
data = json.load(open(root+file))
if data != None:
url = data['url']
urls.remove(url)
index.remove(i)
elif data == None:
os.remove(root+file)
print(f"total data {len(urls)}")
j = 1
for i,url in zip(index,urls):
dest_file = dest_folder + f"{i:05d}.json"
try:
start = time.time()
req = requests.get(url,headers=logic.headers)
result = logic.scraper(url, req.text)
if not os.path.exists(dest_file) and result != None:
json.dump(result,open(dest_file,"w"))
end = time.time()
speed.append(end-start)
est = af.sec2hms(int(np.mean(speed)*(len(index)-j)))
print_green(f"{i:05d}: {end-start:.2f} sec (est. {est} left)")
except:
print_red("failed")
j += 1
# end statement
end = time.time()
time_str = f"{af.sec2hms(end-beginning)}"
print_yellow("done in " + time_str)