-
Notifications
You must be signed in to change notification settings - Fork 0
/
paginated_webpages.py
64 lines (39 loc) · 1.41 KB
/
paginated_webpages.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
from bs4 import BeautifulSoup
import requests
def get_singles_links(url):
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text,'lxml')
for link in soup.findAll('a'):
hrf = link.get('href')
if url == str(hrf):
continue
elif check_url(hrf) and ( kwrd in str(hrf) or kwrd in link.get_text() ) :
unq_links.add(hrf)
# and str(hrf).startswith(base_url)
def check_url(hrf):
if 'javascript' in hrf or str(hrf).startswith('#'):
return False
else:
return True
n = int(input("Enter number of pages: "))
kwrd = input("Enter Keyword: ").strip()
base_url = input("Enter URL: ").strip()
pg = 1
unq_links = set()
while pg <= n:
url = base_url + str(pg)
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text,'lxml')
for link in soup.findAll('a'):
hrf = link.get('href')
if url == str(hrf):
continue
elif check_url(hrf) and ( kwrd in str(hrf) or kwrd in link.get_text() ) :
unq_links.add(hrf)
get_singles_links(hrf)
#and str(hrf).startswith(base_url)
pg += 1
print('\n\n'+str(len(unq_links)))
print('\n\n'.join(unq_links))