The split () function in Python, typically used to slice and convert a string into a list.
First, function Description:
Split (): Syntax: Str.split (str= "", Num=string.count (str)) [n]
Splits a string. Slice the string by making a delimiter and return the segmented list of strings [list]
Parameter: str: delimiter, default is a space, but cannot be empty ("")
Num: Indicates the number of splits. If NUM is specified, it is split into n+1 substrings, and each string can be assigned to a new variable
[n]: Select the nth Shard, which is the nth string, calculated from 0.
1 #Coding=utf-82 Import Time3 fromSeleniumImportWebdriver4 5 6 classgetsubstring (object):7 8 defGet_search_result (self):9Driver =Webdriver. Chrome ()Ten Driver.maximize_window () OneDriver.implicitly_wait (8) A -Driver.get ('https://www.baidu.com') -DRIVER.FIND_ELEMENT_BY_ID ('kw'). Send_keys ('Selenium') theTime.sleep (1) -search_result_string = Driver.find_element_by_xpath ("//*/div[@class = ' nums ')"). Text - Print(search_result_string) - +new_string = Search_result_string.split ('about') [1]#first cut to get XXXX, [1] for cutting the right part - Print(new_string) +Last_result = New_string.split ('a') [0]#second cut, get the number we want [0] represents the left part of the cut reference parameter A Print(Last_result) at - -GetString =getsubstring () -Getstring.get_search_result ()
Python's String cutting