-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathir-indegree.py
More file actions
88 lines (75 loc) · 1.95 KB
/
Copy pathir-indegree.py
File metadata and controls
88 lines (75 loc) · 1.95 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
84
85
86
87
88
from bs4 import BeautifulSoup
import requests
import math
# crawl_link="http://www.amazon.com"
crawl_link="http://www.sridhama.com"
links=[]#list to store all the links crawled
links.append(crawl_link)
dict_out={}#key:val is link:links in the link
indegree={}#link:indegree
outdegree={}
d=0
def getdomain(link):
if "http" in link:
parts=link.split("/")
link= parts[2]
return link
# def checkwwwurl(s):
# if "www" in s:
# return s
# else:
# parts=s.split("//")
# part1=parts[0]
# part2="www."+parts[1]
# return part1+"//"+part2
def crawl(url):
soup = BeautifulSoup(requests.get(url).text,"lxml")
dict_out[url]=[]
for link in soup.find_all('a'):
addr=link.get('href')
addr=str(addr)
try:
if(addr[:4]=="http"): # and (len(links)<=1000):
links.append(addr)
dict_out[url].append(addr)
# print("added")
else:
continue
except Exception as e:
print(addr)
print(e)
return dict_out
for link in links:
if(d<=100):
d=d+1
crawl(link)
for link in links:#to get how many links point to link in dict
indegree[link]=0
for key in dict_out.keys():
if key!=link:
if link in dict_out[key]:
indegree[link]+=1
for link in links:
outdegree[link]=0
for key in dict_out.keys():
if key==link:
outdegree[link]=len(key)
print(dict_out)
# print("====================")
print("LINKS: ")
print(links)
print("==============")
print("INDEGREES: ")
print(indegree)
print("==============")
print("OUTDEGREES:")
print(outdegree)
quiz={k:None for k in outdegree.keys()}
for link in links:
try:
quiz[link]=(indegree[link]/outdegree[link])
except ZeroDivisionError:
quiz[link]=math.inf
print("============")
for i in sorted(quiz, key=quiz.get,reverse=True):
print(i,quiz[i])