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()