Target: Python string cutting operation.
In Python comes with a cutting method split (), this method takes no parameters, the default Andrews Space to cut the field, if the band parameters, in accordance with the parameters to cut.
Exercise scenarios: Baidu search "selenium", to see how many results are found, we need to separate out the removal of this figure.
Specific code:
# coding=utf-8 import time from selenium import webdriver from selenium.webdriver.common.keys import Keys 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') time.sleep(1) driver.find_element_by_id('kw').send_keys(Keys.ENTER) time.sleep(1) search_result_string = driver.find_element_by_xpath("//*/div[@class='nums']").text print(search_result_string) new_string = search_result_string.split('约')[1] # Xxxx a first cut obtained, [1] represents the right side of the cutting portion print(new_string) last_result = new_string.split('个')[0] # Second cut, we want to get digital [0] represents the cutting parameters with reference to the left part of print(last_result) getstring = GetSubString() getstring.get_search_result()
operation result:
Reference article: https://blog.csdn.net/u011541946/article/details/70184257