Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter.simpledialog import askstring
- import tkinter.filedialog, urllib.request, urllib.error, sys, re
- def getUrl():
- urltoopen = askstring('Address', 'Enter A Site')
- urltoopen.strip()
- if not urltoopen.lower().startswith("http://"):
- urltoopen = "http://" + urltoopen
- return urltoopen
- def readPage(urltoopen):
- try:
- html = urllib.request.urlopen(urltoopen).read().decode('utf-8')
- body = re.findall(r'<body.*?>(.*?)</body>', html, re.DOTALL)
- except urllib.error.URLError as e:
- print("Error accessing URL",e.reason)
- sys.exit(0)
- return body
- def getLinks(body):
- newList = []
- final = set()
- links = re.findall(r'<a.*?(.*?)</a>',body[0], re.DOTALL)
- href = re.findall(r'href="(.*?)"',"".join(links), re.DOTALL)
- for httpLinks in href:
- newList.append(re.findall(r'http.*',httpLinks, re.DOTALL))
- for a in newList:
- if a != list():
- final.add(a[0])
- return final
- def writetofile(saveToFile, links):
- for a in links:
- saveToFile.write(a)
- saveToFile.write('\n')
- def run():
- root = Tk()
- root.withdraw()
- site = getUrl()
- body = readPage(site)
- links = getLinks(body)
- saveToFile = tkinter.filedialog.asksaveasfile()
- writetofile(saveToFile, links)
- saveToFile.close()
- if __name__ == "__main__":
- run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement