-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnanoscrape.py
More file actions
59 lines (45 loc) · 1.95 KB
/
Copy pathnanoscrape.py
File metadata and controls
59 lines (45 loc) · 1.95 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#Main module that does everything.
from objs.scraper import Scraper
import importlib, argparse
from urllib.parse import urlparse
import objs.globals
def nanoscrape():
parser = argparse.ArgumentParser("nanoscrape")
parser.add_argument("url", help="URL to scrape from")
parser.add_argument("-d", "--directory", help="relative path to a custom directory to save the scraped images. Defaults to 'images'.")
parser.add_argument("-u","--username",help="Username for login",default=None)
parser.add_argument("-p","--password",help="Password for login",default=None)
args = parser.parse_args()
scraper: Scraper = None
# match scraper to correct subclass by url domain name
url = urlparse(args.url)
scraper_class = ""
# dynamically load module based on URL domain
try:
scraper_class = objs.globals.DOMAINS[url.hostname]
except KeyError:
print(f"URL {url.geturl()} not recognized by nanoscrape. Please check and try again.")
return
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("~~~~~~~~~~NANOSCRAPE~~~~~~~~~~")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("nanoscrape-python version 5/2026.")
# load scraper functions for detected website
scraper_class = getattr(importlib.import_module(scraper_class),"ScraperImpl")
scraper = scraper_class(url.geturl())
# setup scraper.dir
scraper.dir = "images" if args.directory is None else args.directory
# do the scrape
try:
scraper.load_page()
# login functions should come with built-in check for login elements
if not scraper.login(args.username, args.password):
raise objs.globals.BadLogin()
print("-> Login phase complete.")
scraper.get_pages()
except Exception as e:
print(e)
finally:
scraper.save_pages()
print("\n--Scrape complete--\n")
nanoscrape()