beautifulGroup输出在.strip()之后包含空格

2024-09-30 10:38:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码。在

html = urllib2.urlopen(
    'https://ebet.tab.co.nz/results/CHCG-reslt05070400.html').read()


soup = BeautifulSoup(html)
data = soup.findAll('div', {'class' : 'header bold'})
match = re.search('R', data[0].text)
race_title = data[0].text[(match.start()):]
race_title = str(race_title.strip(' \t\n\r'))
print race_title

我在控制台屏幕上得到的输出如下所示

^{pr2}$

我以为strip可以消除SPRINT和C0之间的任何类型的空格,但显然我遗漏了一些东西,所以我需要帮助来理解这个结果。是因为bs4以unicode格式输出字符串吗?在


Tags: 代码texthttpsdatatitlehtmlmatchurllib2
2条回答

通常STRIP只剥离字符串开头和结尾的空格。 使用STR_REPLACE将空格替换为“”。在

strip( s[, chars]) Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, whitespace characters are removed. If given and not None, chars must be a string; the characters in the string will be stripped from the both ends of the string this method is called on. Changed in version 2.2.3: The chars parameter was added. The chars parameter cannot be passed in earlier 2.2 versions.

strip()只删除前导字符或尾随字符。如果要删除换行符,应该使用replace("\n","")

相关问题 更多 >

    热门问题