我正在编写一个使用的程序 的urllib2 从http站点下载CSV数据。该程序在Python中运行时工作正常,但我也在尝试使用 argparse 能够从命令行输入URL。
运行时出现以下错误:
File "urlcsv.py", line 51, in downloadData
return urllib2.urlopen(url)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 396, in open
protocol = req.get_type()
AttributeError: 'Namespace' object has no attribute 'get_type'
我想这是其中的一部分 的urllib2 库因为它不是我写的代码。 是否有其他人遇到过类似的问题 argparse 要么 的urllib2 模块?
代码的相关部分如下:
parser = argparse.ArgumentParser()
parser.add_argument("url")
def main():
"""Runs when the program is opened"""
args = parser.parse_args()
if args is False:
SystemExit
try:
csvData = downloadData(args)
except urllib2.URLError:
print 'Please try a different URL'
raise
else:
LOG_FILENAME = 'errors.log'
logging.basicConfig(filename=LOG_FILENAME,
level=logging.DEBUG,
)
logging.getLogger('assignment2')
personData = processData(csvData)
ID = int(raw_input("Enter a user ID: "))
if ID <= 0:
raise Exception('Program exited, value <= 0')
else:
displayPerson(ID)
main()
def downloadData(url):
return urllib2.urlopen(url)