Python 从美化组替换\n\t
您好,我正在使用BeautifulSoup 4,我尝试替换soup文本中的“\n\t”字符 这是我的密码:Python 从美化组替换\n\t,python,replace,beautifulsoup,special-characters,Python,Replace,Beautifulsoup,Special Characters,您好,我正在使用BeautifulSoup 4,我尝试替换soup文本中的“\n\t”字符 这是我的密码: soup = BS(html_doc, "html.parser") for tableItem in soup.find_all("td"): result = str(tableItem.string) result = result.replace("\n\t\", "") print(result) 这是我的作品: \n', '\t\t\t\t\t\t\t
soup = BS(html_doc, "html.parser")
for tableItem in soup.find_all("td"):
result = str(tableItem.string)
result = result.replace("\n\t\", "")
print(result)
这是我的作品:
\n', '\t\t\t\t\t\t\t\t\t\tTEXT_I_WANT\t\t\t\t\t\t\t\t\t
我尝试了一些关于编码或beautifulsoup“Navigablesting”的方法。我是否使用了错误的编码?或者有没有特殊的方法来美化群体。(如剥去的线绳)
ps:我可以替换所需的文本,但不能替换“\n”或“\t”这行:
result=result.replace(“\n\t\”,”)
查找\n\t
的所有实例,然后替换它们-它不查找\n
或\t
的单个实例。看来你想要的是:
result = result.replace('\n', '')
result = result.replace('\t', '')
实际上,您需要的不是字符串get_text()
还可以删除文本开头和结尾的\n
和\t
:
soup = BS(html_doc, "html.parser")
for tableItem in soup.find_all("td"):
print(tableItem.get_text(strip=True))
使用string strip()方法不幸的是strip()对neitherHi没有帮助,谢谢这对我现在起作用了:result=result.replace('\\n','')。我需要两次“\\”,但我也不得不用两个电话分开替换