Understanding of Python strip method
1, cause
After getting in touch with the crawler, crawl the news headlines of a certain website, and characters such as '\t', '\r', '\n' will appear at both ends. I like to use them for beginners .replace('\t', '').replace('\r', '').replace('\n', '')
. '\r', '\n' and other characters are set to empty, although this can achieve the desired result, but the amount of code is large and the code is very long, so it is not recommended
2. Improve and understand
One time, when I was browsing the code of the big guys on the Internet, I found that the crawlers they wrote like to add after the xpath of the crawling title .strip()
. After a search, I found the usefulness of strip()
2.1 Usage
The default is to cut off spaces at both ends of the string, including '\t', '\r', '\n'
After adding the parameters, the characters specified at the beginning and end of the string are removed.
2.2 Grammar
The strip() syntax in Python:s.strip([chars])
2.3 Sample