-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
77 lines (63 loc) · 2.39 KB
/
Copy pathmain.py
File metadata and controls
77 lines (63 loc) · 2.39 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""
Extract books's datas from website http://books.toscrape.com/, depending on several
criteria
"""
import argparse
from extract import extract_book_datas
from extract import extract_book_datas_by_category
from extract import extract_all
from export_csv import export_csv
from images import images_download
def main():
livres = []
parser = argparse.ArgumentParser(
description="extract book's datas "
"http://books.toscrape.com/")
parser.add_argument("--csv", action="store_true",
help="save datas into csv file")
parser.add_argument("--all", action="store_true",
help="extract all books ")
parser.add_argument("--url",
help="url of the page from which to extract the datas")
parser.add_argument("--images", action="store_true",
help="Save images into data/images")
parser.add_argument("--impact", choices=['book', 'cat', 'all'],
help="Extract book's data with type=book "
"or from category with type=cat "
)
args = parser.parse_args()
if args.url and (args.impact is None):
parser.error("--url requiert --impact.")
if args.csv and ((args.impact is None or args.url is None) and not args.all):
parser.error("--csv requiert --impact et --url, or --all.")
if args.impact and (args.url is None):
parser.error("--impact requiert --url.")
try:
if args.url and args.impact == 'book':
livre = extract_book_datas(args.url)
if livre:
livres.append(livre)
if args.csv:
export_csv(livres, 'category')
else:
print(livre)
elif args.url and args.impact == 'cat':
livres = extract_book_datas_by_category(args.url)
if livres:
if args.csv:
export_csv(livres, 'category')
else:
print(livres)
elif args.all:
livres = extract_all()
if livres:
if args.csv:
export_csv(livres, 'category')
else:
print(livres)
if args.images:
images_download()
except ValueError as exc:
print(exc)
if __name__ == '__main__':
main()