我正在编写一个程序,使用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)
urllib2
模块中的问题,这是从给定的路径中得出的结论。 - nbroreq
设置为什么了? - user559633protocol = req.get_type()
是Python标准库中urllib2模块的一部分; 这不是我编写的代码,而是由urllib2.urlopen(url)
调用的代码。 - Sam Cohen-Devriesprotocol=req.get_type()
的行,这意味着您仍然需要“发布代码”,以便说。我不知道您希望req存在的位置,因为没有成员方法urllib2.req
。 - user559633