-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_links.py
31 lines (25 loc) · 879 Bytes
/
get_links.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
import requests
from bs4 import BeautifulSoup
from urllib.parse import urljoin
class get_links:
def __init__(self, main_url) -> None:
self.main_url = main_url
def gather_links(self, init_link):
try:
html_text = requests.get(init_link).text
soup = BeautifulSoup(html_text, 'lxml')
all_links = soup.find_all('a')
list = []
for link in all_links:
l = link.get('href')
l = urljoin(self.main_url, l)
if self.is_home_link(l):
list.append(l)
return list
except:
print("Error Opening Link. Try again")
def is_home_link(self, link):
length = len(self.main_url)
if(self.main_url == link[0:length]):
return True
return False