Passing a url as argument

爷,独闯天下 提交于 2021-02-04 20:49:49

问题


Whenever I try to run the script as below, I get the following results. What is wrong with the code?

1.python test.py

doesn't print the usage.

2.python test.py http://link.com/index.php?title=tesst&action=raw

prints: "'action' is not recognized as an internal or external command, operable program or batch file."

My script:

# Version YYYYMMDD
version = "20121112"

# File type to be output to logs
# Should be changed to exe before building the exe.
fileType = "py"

# Import sys to read command line arguments
import sys, getopt
#import pdb
#pdb.set_trace()

import argparse

def update (url):
    req = urllib2.Request(url=url)
    try:
        f = urllib2.urlopen(req)
        txt = f.read()
        f.close()
    except urllib2.HTTPError, e:
        txt = ''
        print 'An error occured connecting to the wiki. No wiki page will be generated.'
        return '<font color=\"red\">QWiki</font>'
    # Find the start tag of the textarea with Regular Expressions
    p = re.compile('<textarea[^>]*>')
    m = p.search(txt)
    (tagStart, tagEnd) = m.span()
    # Find the end of the textarea
    endTag = txt.index("</textarea>")

def main ():
    #For logging
    print "test"
    parser = argparse.ArgumentParser(description='This is the update.py script created by test')
    parser.add_argument('-u','--ur',action='store',dest='url',default=None,help='<Required> url link',required=True)
    results = parser.parse_args()# collect cmd line args
    url = results.url
    print url
    update(url)

回答1:


Wrap the URL in quotes:

python test.py "http://link.com/index.php?title=tesst&action=raw"

The & is confusing Command Prompt.



来源:https://stackoverflow.com/questions/15170547/passing-a-url-as-argument

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!