我有這樣的代碼,Django的嘗試除了不工作
try:
print "what"
newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = nameAndNumberStore[0])
print newClassName
except:
print "HAHA"
這始終打印「哈哈」,儘管我已經運行在控制檯newClassName = ...代碼和它的工作的事實。
這是怎麼發生的?
編輯
def newGetAllInformation(searchTerm):
nameAndNumberStore = modifySearchTerm(searchTerm)
urlStore = modifyUrl(nameAndNumberStore) # need to make the change here -- why not I go to the site, check for Course name - if that is not there switch, if it is then scrape
soup = getHtml(urlStore)
storeOfBooks = []
storeOfBooks = scrape(soup,nameAndNumberStore)
print nameAndNumberStore[0]
try:
newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = nameAndNumberStore[0])
nameAndNumberStore = modifySearchTerm(newClassName.departmentName + " " + nameAndNumberStore[1])
urlStore = modifyUrl(nameAndNumberStore)
soup = getHtml(urlStore)
storeOfBooks = scrape(soup,nameAndNumberStore)
except:
print "HAHA"
return storeOfBooks
編輯 經過進一步調查 - 也就是說,手動(這工作),我覺得有什麼東西了以代碼從陣列 - 即使輸入有效的代碼兩者都是相同的數據類型(字符串)的事實。從文件
所以newClassName = CourseNameAndCodeAssociation.objects.get(departmentCode__iexact = "econ")
的作品,但newClassName = CourseNameAndCodeAssocition.objects.get(departmentCode__iexact = nameAndNumberStore[0])
,其中nameAndNumberStore[0]
持有經濟學
你有沒有試過,你知道...讓異常通過? –
我注意到你的代碼片段沒有任何導入。也許如果你向我們展示你的整個控制檯會話,以及影響你的環境的所有代碼,我們可以提供幫助。 – Marcin
呃,我正在導入BeautifulSoup,re,urllib2和cookielib - 這個函數有很多函數被整體調用 - 但我會發布整個東西 你是什麼意思讓異常通過?我對編程有點新奇 - 我不需要去捕捉它嗎? – praks5432