Python学习报错笔记--20190901

1.TypeError: 'builtin_function_or_method' object is not subscriptable
方法使用错误引起的报错,这个错误的本质原因是因为个人使用split()方法的错误导致的,将()失误输成了“[]”,纠正后不再报错。

>>> a="10k-20k 经验应届毕业生 本科"
>>> print(a.split[' '])
Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    print(a.split[' '])
TypeError: 'builtin_function_or_method' object is not subscriptable

因为这个报错,回顾一下split()方法的使用 

str.split(str="", num=string.count(str))

其中str表示你以什么字符去分割字符串,可以是空格、换行(\n)、制表符(\t)等

num表示你想将字符串分割为几段,默认值为-1,意思是在字符串中遇到str的值就分割,但如果是num=1就要在遇到的第一个str处分割,也就是分割为两段。

2.UnboundLocalError: local variable 'next_page' referenced before assignment

在“下一頁”按鍵置灰时,錯誤的使用try:...except xx:...

错误代码:

def next_page():
    try:
        next_page = driver.find_element_by_xpath('//span[@class = "pager_next "]')
    except NoSuchElementException:
        driver.quit()
    
    ActionChains(driver).move_to_element(next_page).perform()
    sleep(2)
    next_page.click()

当try中获取元素的语句失败时,执行了except中动作,之后又会执行ActionChains(driver).move_to_element(next_page).perform(),此时next_page无值,因此报错

 

解决:将后三句加入到“else”中,即使用try:...except xx:... else:...

def next_page():
    try:
        next_page = driver.find_element_by_xpath('//span[@class = "pager_next "]')
    except NoSuchElementException:
        driver.quit()
    else:
        ActionChains(driver).move_to_element(next_page).perform()
        sleep(2)
        next_page.click()

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值