尝试/除了... AttributeError:只能将.str访问器与字符串值一起使用

时间:2020-05-29 00:05:24

标签: python pandas selenium web-scraping

我正在运行一个功能,该功能可以从网站上获取一些数据并将其写入熊猫数据库。我正在使用硒和geckodriver。

...code...

first_names = driver.find_elements_by_class_name('first-name')
first_names = [name.text for name in first_names]

last_names = driver.find_elements_by_class_name('last-name')
last_names = [name.text for name in last_names]

commit_status = driver.find_elements_by_class_name('school-name')
commit_status = [commit.text for commit in commit_status]

#error is happening below

athlete['commit_school'] = athlete['commit'].str.replace('\d+', '').str.replace('/', 
'').str.replace('VERBAL', '').str.replace('SIGNED', '')

athlete['first'] = athlete['first'].str.title()
athlete['last'] = athlete['last'].str.title()

...code...

然后,我循环浏览此功能,以浏览不同状态网页上的相似数据。有时它正常返回页面上的数据并继续到下一个状态,而其他时候,我得到: AttributeError:只能将.str访问器与字符串值一起使用! ...然后代码中断。让我感到困惑的部分是,我得到错误的时间似乎是任意的。有时我会通过1/4的循环,有时会通过3/4的循环。

我第一次尝试修复是try / except,但是我不确定我做对了还是最好的方法:

athlete['state'] = state_all[:length:]
athlete['hs'] = hs_all[:length:]
athlete['commit'] = commit_status[:length:]

try:
    athlete['commit_school'] = athlete['commit'].str.replace('\d+', '').str.replace('/', 
    '').str.replace('VERBAL', '').str.replace('SIGNED', '')
    athlete['first'] = athlete['first'].str.title()
    athlete['last'] = athlete['last'].str.title()
except AttributeError:
    pass

athlete['list'] = 'Rivals_' + year + '_' + list_state
athlete['home'] = profile_page[:length:]

该try / except语句中发生了错误,但是我认为如果发现错误,它只会跳过所有错误。

1 个答案:

答案 0 :(得分:1)

下面的代码在我为每列添加.astype('str')的中间是否解决?您可能具有混合了字符串和对象数据类型的列。

athlete['commit_school'] = athlete['commit'].astype('str').str.replace('\d+', '').str.replace('/', '').str.replace('VERBAL', '').str.replace('SIGNED', '')

athlete['first'] = athlete['first'].astype('str').str.title()
athlete['last'] = athlete['last'].astype('str').str.title()