forked from Py-Contributors/awesomeScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrap.py
More file actions
63 lines (48 loc) · 1.7 KB
/
Copy pathscrap.py
File metadata and controls
63 lines (48 loc) · 1.7 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
import requests
import argparse
from bs4 import BeautifulSoup, SoupStrainer
from validator_collection import checkers
parser = argparse.ArgumentParser(description='Scrape them all!')
parser.add_argument('link', metavar='link',
type=str, nargs=1, help='Link of the target website')
parser.add_argument('-o', metavar='outputFile',
type=str, nargs=1, help='Outputs results into a file')
args = parser.parse_args()
class Scraper:
def __init__(self, args):
self.args = args
self.greet()
def greet(self):
print('''
Scrap All Links!
from github.com/Py-Contributors/awesomeScripts
''')
def scrape(self, args):
links = []
link_arg = args.link[0]
if not checkers.is_url(link_arg):
raise Exception(link_arg + " is not a valid URL")
response = requests.get(link_arg)
for link in BeautifulSoup(response.text, features="html.parser",
parse_only=SoupStrainer('a')):
if link.has_attr('href') and link['href'].startswith("http"):
links.append(link['href'])
return links
def pretty_print(self, links):
out = False
outPath = str
if self.args.o:
out = True
outPath = "./" + self.args.o[0]
open(outPath, 'w').close()
for link in links:
print(link)
if out:
with open(outPath, 'a') as f:
f.write(link + "\n")
f.close()
def run(self):
self.pretty_print(self.scrape(self.args))
if __name__ == "__main__":
scraper = Scraper(args)
scraper.run()