-
Notifications
You must be signed in to change notification settings - Fork 0
/
alllinks.py
74 lines (73 loc) · 2.07 KB
/
alllinks.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
#scrape all links linked in a web site
#ALL 4 FUNCTIONS DO THE SAME JOB
import requests
import re
import lxml.html
import lxml
from selectolax.parser import HTMLParser
k=input("Enter URL to parse - ")
try:
r=requests.get(k)
except:
r = requests.get("http://"+k)
k="http://"+k
def extractlink(s):
l=[]
dom = HTMLParser(s)
for tag in dom.tags("a"):
attrs=tag.attributes
if "href" in attrs:
if str(attrs["href"]).startswith(r"/"):
l.append(k + attrs["href"])
else:
if attrs["href"].startswith(r"http"):
l.append(attrs["href"])
elif attrs["href"]:
l.append(k + r"/" + attrs["href"])
return l
def linkextractor(s):
t=re.compile(r'<a[^<>]+?href=([\'\"])(.*?)\1',re.IGNORECASE)
l=[]
l1=[match[1] for match in t.findall(s)]
for i in l1:
if str(i).startswith(r"/"):
l.append(k + i)
else:
if i.startswith(r"http"):
l.append(i)
elif i:
l.append(k + r"/" + i)
return l
def linkextractorlxml(s):
l=[]
dom=lxml.html.fromstring(s)
for i in dom.xpath('//a/@href'):
if str(i).startswith(r"/"):
l.append(k + i)
else:
if i.startswith(r"http"):
l.append(i)
elif i:
l.append(k + r"/" + i)
return l
from bs4 import BeautifulSoup as bs
def extractbs(s):
l=[]
soup=bs(s,"lxml")
for tag in soup.findAll("a",href=True):
if str(tag["href"]).startswith(r"/"):
l.append(k+tag["href"])
else:
if tag["href"].startswith(r"http"):
l.append(tag["href"])
elif tag["href"]:
l.append(k+r"/"+tag["href"])
return l
for i in extractbs(r.text):
print("URLS of the website - " , i)
for i in extractlink(r.text):
print("URLS of the website - " , i)
for i in linkextractor(r.text):
print("URLS of the website - " , i)
for i in linkextractorlxml(r.text):
print("URLS of the website - " , i)