【问题标题】:Passing a url as argument将 url 作为参数传递
【发布时间】:2013-05-21 20:46:02
【问题描述】:

每当我尝试运行如下脚本时,都会得到以下结果。代码有什么问题?

1.python test.py

不打印使用情况。

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

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

我的脚本:

# 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)

【问题讨论】:

    标签: python command-line-arguments


    【解决方案1】:

    将 URL 用引号括起来:

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

    &amp; 令人困惑的命令提示符。

    【讨论】:

    • @blendar - 如果我不传递任何参数..为什么不打印用法?
    • 也将 url 打印为“打印 url”,它只是不打印任何内容
    猜你喜欢
    • 1970-01-01
    • 2011-12-19
    • 2010-12-18
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    相关资源
    最近更新 更多