forked from Lrantala/SimpleWebScraper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebScraper.py
More file actions
83 lines (70 loc) · 2.87 KB
/
WebScraper.py
File metadata and controls
83 lines (70 loc) · 2.87 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
78
79
80
81
82
83
import sys
from bs4 import BeautifulSoup
import requests
def get_website_contents(website_address):
"""This function retrieves and returns the contents of the
website that is passed to it as an argument."""
website = requests.get(website_address)
try:
website.raise_for_status()
except Exception as exception:
print("Error encountered: %s" % exception)
return None
return website.content
def get_lyrics_between_tags(contents, tags):
"""This function gets the song texts between certain tags"""
soup = BeautifulSoup(contents, 'html.parser')
lyrics = soup.find("div", attrs={"class": tags})
lyrics_text = lyrics.text.strip()
return lyrics_text
def write_website_to_file(contents, filename):
"""This function writes the website contents to a file
as bytes."""
write_contents_bytes = bytearray(contents, "utf-8")
try:
with open(filename, "wb") as file:
file.write(write_contents_bytes)
except IOError as exception:
print("Couldn't save the file. Encountered an error: %s" % exception)
def read_arguments(arguments):
"""This reads the arguments passed to the program and returns
the latter representing the website address. If no address is
provided, the function returns None."""
if len(arguments) == 3:
return arguments[1], arguments[2]
elif len(arguments) == 2:
return arguments[1], None
else:
return None, None
def print_help():
"""This funcion prints help text to instruct user how to use
this scraping program."""
print("\nThis is a simple program for scraping websites.\n"
"To scrape a website, pass it as an argument and optionally\n"
"pass also the name of the file you want the results to be saved\n"
"as a second argument.\n"
"Example 1. WebScraper.py address_of_the_website\n"
"Example 2. WebScraper.py address_of_the_website save_contents_here")
if __name__ == "__main__":
address, filename = read_arguments(sys.argv)
if address is None:
print_help()
else:
raw_content = get_website_contents(address)
content = get_lyrics_between_tags(raw_content, "lyrics")
if content is None:
print("Website does not exist")
else:
if filename is not None:
write_website_to_file(content, filename)
else:
while True:
user_choice = input(
"Give a name where the contents of the website will be saved, or (q)uit: ").strip()
if user_choice == "Q" or user_choice == "q":
break
elif user_choice == "":
print("You have to insert a filename.")
else:
write_website_to_file(content, user_choice)
break