forked from TomRichtr/training-creations_python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
IPanalyzer.py
55 lines (37 loc) · 1.41 KB
/
IPanalyzer.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
def ipanalyzer ():
#import libraries needed for html scraping
import requests
import urllib.request
from bs4 import BeautifulSoup
#definition of website from which will data be scraped
url = "https://www.myip.com/"
#checking accessability of website, 200 okay, 403 forbidden
response = requests.get (url)
#class for library assignment to variable
soup = BeautifulSoup (response.text,"html.parser")
#function used from library to find all "span" tags
soup.findAll ("span")
#creation of variable from tag containing IP
iptag = soup.findAll ("span") [0]
#cration of string containing IP, changing to list divided by signs <,> and then pulling a element containing only ip
ipstr = str (iptag)
ipstr = ipstr.split (">") [1]
ipstr = ipstr.split ("<") [0]
#the same for country, town and service provider
countrytag = soup.findAll ("div") [23]
countrystr = str (countrytag)
countrystr = countrystr.split (">") [1]
countrystr = countrystr.split ("<") [0]
sptag = list (soup.findAll ("div") [17]) [2]
spstr = sptag.split ("\n") [1]
spstr = spstr.replace (". ",".")
print ("IP is: " + ipstr + "\n"
"Registered in: " + countrystr + "\n"
"Provided by: " + spstr)
while True:
ipanalyzer ()
reply = input ("Again? Y/N")
if reply.lower () [0] == "y":
continue
else:
break