使用python如何摆脱从网站上抓取的文本中的尾随空格[重复]
Posted
技术标签:
【中文标题】使用python如何摆脱从网站上抓取的文本中的尾随空格[重复]【英文标题】:using python how do you get rid of trailing spaces in text that was scraped from a website [duplicate] 【发布时间】:2021-03-07 20:33:09 【问题描述】:我正在从网站上抓取文本,我正在使用 python 中的 strip 函数来删除这些换行符和尾随空格,并将所有逗号替换为空格。但是它什么也没做
# print(predesc[0].div.p)
itemDesc = predesc[0].div.p.text
# itemDesc= str(itemDesc).strip("\\t\\n")
itemDesc.strip()
itemDesc.strip("\t")
itemDesc.strip("\n\n")
itemDesc.replace(",","")
print(repr(itemDesc))```
output is
" Shortcut the learning curve with an all-around board that''s catch-free and easy for boosting confidence anywhere you take it.\n\nSome riders just want to get straight to the fun part. Enjoy a no-fuss feel with the Burton Instigator, a board designed to help accelerate the learning curve and instigate a good time from your first moment on the mountain. The combination of a Flat Top™ bend and Cruise Control convex base keeps things friendly underfoot, creating a catch-free feel that maintains stability and control. The Channel® mounting system gives you the easiest, most adjustable setup with bindings from all major brands (not just Burton''s). "
【问题讨论】:
strip()
返回一个新值...它不会更改原始字符串。你需要像itemDesc = itemDesc.strip()
这样的东西。
将您想要删除尾随空格的内容分配给变量。然后打印出来。
谢谢你,我知道它确实去掉了空格,但 \n\n 没有被条纹
与从任何其他数据中删除尾随空格的方法相同。
【参考方案1】:
itemDesc = itemDesc.replace(' ',' ').replace(',','').replace("''","'").replace("\n","").strip()
【讨论】:
以上是关于使用python如何摆脱从网站上抓取的文本中的尾随空格[重复]的主要内容,如果未能解决你的问题,请参考以下文章