https://blog.youkuaiyun.com/u011541946/article/details/70184257
增加一行:
driver.find_element_by_id('su').click()报错:UnicodeDecodeError: 'ascii' codec can't decode byte 0xe7 in position 0: ordinal not in range(128)
解决办法:在个和约前面加上一个u
# coding=utf-8 import time from selenium import webdriver class GetSubString(object): def get_search_result(self): driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(8) driver.get('https://www.baidu.com') driver.find_element_by_id('kw').send_keys('selenium') driver.find_element_by_id('su').click() time.sleep(1) search_result_string = driver.find_element_by_xpath("//*[@id='container']/div[2]/div/div[2]").text print (search_result_string) new_string = search_result_string.split(u'约')[1] # 第一次切割得到 xxxx个,[1]代表切割右边部分 print (new_string) last_result = new_string.split(u'个')[0] # 第二次切割,得到我们想要的数字 [0]代表切割参照参数的左边部分 print (last_result) getstring = GetSubString() getstring.get_search_result()
在使用Python进行字符串操作时遇到UnicodeDecodeError,具体错误为'ascii' codec无法解码字节0xe7。解决方案是在字符串前添加前缀'u',以正确处理非ASCII字符。
680

被折叠的 条评论
为什么被折叠?



