學(xué)習(xí)使用Django進(jìn)行網(wǎng)頁(yè)爬取取決于你對(duì)Python、Django框架和網(wǎng)絡(luò)爬蟲(chóng)的熟悉程度。以下是一些關(guān)鍵點(diǎn),總的來(lái)說(shuō),如果你已經(jīng)具備Python和Django的基礎(chǔ)知識(shí),并對(duì)網(wǎng)頁(yè)爬蟲(chóng)有一定了解,那么學(xué)習(xí)使用Django進(jìn)行網(wǎng)頁(yè)爬取將會(huì)比較容易。如果你是一個(gè)完全的初學(xué)者,那么可能需要更多的時(shí)間和努力來(lái)掌握所需的所有技能。不過(guò),通過(guò)逐步學(xué)習(xí)和實(shí)踐,這是完全可行的。比如我遇到得下面得問(wèn)題以及我得應(yīng)對(duì)方法。
問(wèn)題背景
在Django代碼中,遇到一個(gè)TypeError: ‘float’ object is not callable的錯(cuò)誤。
這個(gè)錯(cuò)誤發(fā)生在幾個(gè)property裝飾器的方法中,例如:
@property
def pmt_loaner_final(self):
return float(self.pmt_loaner_new) + float(self.debit_fee)
@property
def pmt_broker_final(self):
return float(self.pmt_broker_new) + float(self.debit_fee)
@property
def total_compounded_broker(self):
return self.compounded_amount(self.brokerage_fees)
@property
def total_compounded_loaner(self):
return self.compounded_amount(self.amount)
這些property裝飾器的方法試圖將浮點(diǎn)數(shù)轉(zhuǎn)換為整數(shù),但由于浮點(diǎn)數(shù)不是可調(diào)用的對(duì)象,因此拋出TypeError: ‘float’ object is not callable的錯(cuò)誤。
另外,在以下代碼段中,也遇到了同樣的錯(cuò)誤:
@property
def discount(self):
return self.final_credit_rate(
self.pmt_loaner_final + self.pmt_broker_final,
self.total_compounded_loaner + self.total_compounded_broker
)
在該代碼段中,試圖將a+b和c+d的和作為實(shí)參傳遞給final_credit_rate方法,但是由于a+b和c+d都是浮點(diǎn)數(shù),因此拋出TypeError: ‘float’ object is not callable的錯(cuò)誤。
除此之外,還嘗試使用final_pmt_without_withdrawal_fees和total_compounded作為實(shí)參傳遞給get_final_credit_rate方法,也遇到了同樣的錯(cuò)誤。
解決方案
對(duì)于這個(gè)問(wèn)題,有兩種可能的解決方案:
1、移除@property裝飾器
如果不需要將這些方法作為property屬性來(lái)使用,可以移除@property裝飾器,并將這些方法定義為普通的函數(shù)。這樣就可以直接調(diào)用這些方法,而不會(huì)拋出TypeError: ‘float’ object is not callable的錯(cuò)誤。
2、使用括號(hào)調(diào)用property屬性
如果需要將這些方法作為property屬性來(lái)使用,可以使用括號(hào)來(lái)調(diào)用這些屬性。例如:
discount = computation.final_credit_rate(
computation.pmt_loaner_final() + computation.pmt_broker_final(),
computation.total_compounded_loaner() + computation.total_compounded_broker()
)
通過(guò)使用括號(hào)來(lái)調(diào)用property屬性,就可以避免TypeError: ‘float’ object is not callable的錯(cuò)誤。
對(duì)于第二個(gè)問(wèn)題,需要將a+b和c+d的和轉(zhuǎn)換為整數(shù),然后再作為實(shí)參傳遞給final_credit_rate方法。例如:
discount = computation.final_credit_rate(
int(computation.pmt_loaner_final()) + int(computation.pmt_broker_final()),
int(computation.total_compounded_loaner()) + int(computation.total_compounded_broker())
)
通過(guò)將a+b和c+d的和轉(zhuǎn)換為整數(shù),就可以避免TypeError: ‘float’ object is not callable的錯(cuò)誤。
對(duì)于第三個(gè)問(wèn)題,需要將final_pmt_without_withdrawal_fees和total_compounded轉(zhuǎn)換為整數(shù),然后再作為實(shí)參傳遞給get_final_credit_rate方法。例如:
final_credit_rate = computation.get_final_credit_rate(
int(computation.final_pmt_without_withdrawal_fees()),
int(computation.total_compounded())
)
通過(guò)將final_pmt_without_withdrawal_fees和total_compounded轉(zhuǎn)換為整數(shù),就可以避免TypeError: ‘float’ object is not callable的錯(cuò)誤。
了解如何爬取網(wǎng)頁(yè)(使用請(qǐng)求庫(kù)如requests
)、解析HTML(使用解析庫(kù)如BeautifulSoup
或lxml
)是必要的。這部分可以獨(dú)立于Django學(xué)習(xí)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-820737.html
網(wǎng)上有大量的教程和指南,這些資源可以幫助你學(xué)習(xí)Django和爬蟲(chóng)技術(shù)。理論學(xué)習(xí)之后,動(dòng)手實(shí)踐是非常重要的。通過(guò)實(shí)際編寫(xiě)和運(yùn)行代碼來(lái)加深理解。以上就是全部得內(nèi)容,如果有更多學(xué)術(shù)探討,歡迎評(píng)論區(qū)留言討論。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-820737.html
到了這里,關(guān)于Django代碼中的TypeError ‘float‘ object is not callable的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!