Hi,

I'm looking for a little help. I'm trying to create a script with the ability to take home page as an argument, printing a message to say something about the site and then validate whether or not this is a valid URL. I'm relatively new to Python and haven't used arguments very much and would like some help/advice.

I have some basic code which I've completed but I'm unsure of how to expand on it. If someone could help or point me in the direction of where I would find information about arguments and using with with URLs that would be great.

import sys 

def printWebsite(URL):
 URL = raw_input ("Enter website to be checked")
 site = raw_input
 print "Valid URL"  

def main():

 print "Website " 
 printWebsite(sys.argv) 

if __name__ == '__main__': 
 main() 

Any advice would be greatly appreciated.

Your code doesn't make sense as it are now.
If you are gone use sys.argv,you know that means running script from command line and pass in argument from command line?

#site_check.py
import sys

def printWebsite(url):
    print "url for sys.argv {}".format(url)

def main():
    printWebsite(sys.argv[1])

if __name__ == '__main__':
    main()

Running from command line(cmd) of terminal linunx.
python site_check.py www.google.com
This take command line arguments www.google.com and bring it into site_check.py.
sys.arg is name of script,sys.arg[1] is passed in agrument.
Dos this make sense?

Then you can check if url is vaild or not,this means that you have to check status code like "404" or catch exception if site is down.

Always 4 space indentation in Python.

Hi,

I tried using your code however everytime I run it I get the error 'list index out of range' ? Do you know why? I'm currently using Python 2.6

This is the code I'm currently using;

def printWebsite(url):
    url = raw_input("Please enter website")
    print "url for sys.argv {}".format(url)

def main():
    sys.argv.append('http://www.bbc.co.uk')
    if len(sys.argv) != 2:
        print 'Not Valid'
        return
    else:
        print printWebsite(sys.argv[1])

if __name__ == '__main__':
    main()

This is the error I'm getting;

ValueError: zero length field name in format

In python 2.6, you cannot use empty {} in format strings. Use {0} here.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.