Simple Python Program to Extract links from Web Page

link-extract-python

Simple Python Program to Extract links from Web Page

#Author Kumar Shubham 
# http://hackaholic.info
# This program will extract all the links from a webpage
import sys
import urllib2
import re
def find_link(url):
    sys.stdout.write("Wait a Minute its retrieving.")
    try:
        if url[0:4]!="http":
            url="http://" + url
        f=(urllib2.urlopen(url)).read()
        k=re.findall('(src|href)="(\S+)"',f)
        k=set(k)
        print "The Links are:------------"
        for x in k:
            if len(x[1])>2:
                print x[1]
    except:
        print "NOT found plz check url"

def usage():
    print """
_______________________________________________________
|usage:                                                |
|python link.py url                          |
|example:-                                             |
|python link.py http://www.example.com/hello-world     |
--------------------------------------------------------
"""


if __name__ == "__main__":
    print """
 -------------------------------------------------------------
| By Hackaholic                                              |
| http://hackaholic.info                                     |
| subscribe my youtube channel:                              |
| www.youtube.com/juniourkawasakicool                        |
|  www.facebook.com/hackaholic.info                          |
--------------------------------------------------------------"""
    if len(sys.argv)==1:
        usage()
    else:
        find_link(sys.argv[1])

Copy the above Python code and save it as ‘link.py’. Now open terminal(linux) or cmd(‘windows’). Browse to path where you have save the ‘link.py’. Then type:
python link.py http://hackaholic.info

 

Plz Share Share on FacebookShare on Google+Share on LinkedInTweet about this on TwitterEmail this to someoneShare on RedditShare on TumblrDigg thisBuffer this pagePrint this pagePin on PinterestShare on StumbleUponFlattr the author
Bookmark the permalink.

2 Comments

  1. Thanks for this snippet ! I will use it soon. I will try to update it to add more functionalities if you are interested, contact me.

Leave a Reply

Your email address will not be published. Required fields are marked *