-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathscraper.py
88 lines (65 loc) · 1.88 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
#!/usr/bin/python
# scraper.py
# Saito 2017
import argparse
import sys
import urllib
from bs4 import BeautifulSoup
import utils
def scrape(url):
html = urllib.urlopen(url).read()
soup = BeautifulSoup(html, "html.parser")
[x.extract() for x in soup.find_all('script')]
text = soup.get_text(" ", strip=True)
# ftext = text.split(" ")
return text
def scrape_and_save_to_file(url, filename="scraped_text.txt"):
text = scrape(url)
utils.save_file(filename, text)
return text
def get_parser():
""" Creates a command line parser
--doc -d
--help -h
--url -u
--output -o
This automatically grabs arguments from sys.argv[]
"""
parser = argparse.ArgumentParser(
description='Scrape a website.')
parser.add_argument(
'-d', '--doc', action='store_true',
help='print module documentation and exit')
parser.add_argument(
'-u', '--url',
help='''specify a url to scrape. Use the full name like
http://www.cnn.com''')
parser.add_argument(
'-o', '--output',
help='''specify an OUTPUT file to write to.
Default is scraped_text.txt''')
return parser
def main():
parser = get_parser()
args = parser.parse_args()
if args.doc:
print __doc__
sys.exit()
if args.url:
url = args.url
else:
url = "http://chrisnovello.com/teaching/risd/computer-utopias/"
if args.output:
print '\nwriting file to ' + str(args.output)
output_file = args.output
else:
print "\nwriting to scraped_text.txt"
output_file = "scraped_text.txt"
text = scrape_and_save_to_file(url, output_file)
# # Example
# url = "http://chrisnovello.com/teaching/risd/computer-utopias/"
# # text = scrape(url)
# text = scrape_and_save_to_file(url)
print text
if __name__ == '__main__':
main()